vue/no-computed-properties-in-data Correctness
它的作用
禁止在 data() 内访问计算属性。
为什么这不好?
data() 会在计算属性初始化之前运行,因此 this.<computedName> 的值会求值为 undefined,并且会在组件实例中悄悄留下损坏的状态。
示例
以下是此规则的错误代码示例:
vue
<script>
export default {
data() {
const foo = this.foo; // `foo` 是一个计算属性
return {};
},
computed: {
foo() {},
},
};
</script>以下是此规则的正确代码示例:
vue
<script>
export default {
data() {
const foo = this.foo; // `foo` 是一个 prop,不是计算属性
return {};
},
props: ["foo"],
};
</script>如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["vue"],
"rules": {
"vue/no-computed-properties-in-data": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["vue"],
rules: {
"vue/no-computed-properties-in-data": "error",
},
});bash
oxlint --deny vue/no-computed-properties-in-data --vue-plugin版本
此规则于 v1.67.0 中添加。
