Skip to content
← Back to rules

vue/no-side-effects-in-computed-properties Correctness

作用

禁止在计算属性中产生副作用。

为什么这不好?

在计算属性中引入副作用被认为是非常糟糕的做法。 这会使代码变得不可预测且难以理解。

示例

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

vue
<script>
export default {
  computed: {
    fullName() {
      this.firstName = "lorem"; // 副作用
      return `${this.firstName} ${this.lastName}`;
    },
  },
};
</script>

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

vue
<script>
export default {
  computed: {
    fullName() {
      return `${this.firstName} ${this.lastName}`;
    },
  },
};
</script>

如何使用

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

json
{
  "plugins": ["vue"],
  "rules": {
    "vue/no-side-effects-in-computed-properties": "error"
  }
}
ts
import { defineConfig } from "oxlint";

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

版本

此规则在 v1.70.0 中添加。

参考文献