Skip to content
← Back to rules

vue/return-in-computed-property Correctness

它的作用

强制每个计算属性中都存在一个 return 语句。

为什么这很糟糕?

Vue 的计算属性是一个必须生成值的 getter。忘记返回会使值变成 undefined,从而悄无声息地破坏依赖该计算属性的模板和响应式代码。

示例

以下是此规则的错误代码示例:

vue
<script>
export default {
  computed: {
    foo() {
      // 缺少 return
    },
  },
};
</script>

以下是此规则的正确代码示例:

vue
<script>
export default {
  computed: {
    foo() {
      return this.bar;
    },
  },
};
</script>

配置

treatUndefinedAsUnspecified

type: boolean

default: true

当为 true(默认值)时,return;(不带返回值)会被视为缺少返回。 将其设置为 false 可允许单独的 return;,将其视为返回了一个值。

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["vue"],
  "rules": {
    "vue/return-in-computed-property": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["vue"],
  rules: {
    "vue/return-in-computed-property": "error",
  },
});
bash
oxlint --deny vue/return-in-computed-property --vue-plugin

版本

此规则在 v1.63.0 中添加。

参考资料