Vize

script/no-computed-properties-in-data

禁止在组件的 data 函数中使用计算属性。

默认严重程度 预设 自动修复 选项
error essential, happy-path, ecosystem, nuxt, opinionated 不支持 无

适用范围:Vue SFC 中的 JS/TS 脚本;示例展示对应的 Options API 或 script setup 形式。

错误示例

data() 返回 this.doubled,而 doubled 仍是计算属性,读取发生在初始化之前。

<script lang="ts">
export default {
  data() {
    return { value: this.doubled }
  },
  computed: {
    doubled() { return 1 }
  }
}
</script>

正确示例

data() 返回普通的 value,doubled 在初始化之后读取 this.value。

<script lang="ts">
export default {
  data() {
    return { value: 1 }
  },
  computed: {
    doubled() { return this.value * 2 }
  }
}
</script>

配置(Vite+)

import { defineConfig } from "@vizejs/vite-plugin/vite-plus";

export default defineConfig({}).withVue({
  linter: {
    "preset": "incremental",
    "rules": {
      "script/no-computed-properties-in-data": "error"
    }
  },
});

然后运行 vp run lint。

相关页面