noVueDataObjectDeclaration
¥Summary
-
规则生效日期:
v2.1.4¥Rule available since:
v2.1.4 -
诊断类别:
lint/nursery/noVueDataObjectDeclaration¥Diagnostic Category:
lint/nursery/noVueDataObjectDeclaration -
此规则包含 safe 修复程序。
¥This rule has a safe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
此规则属于以下域:
¥This rule belongs to the following domains:
-
来源:
¥Sources:
-
灵感来自
vue/no-deprecated-data-object-declaration¥Inspired from
vue/no-deprecated-data-object-declaration -
灵感来自
vue/no-shared-component-data¥Inspired from
vue/no-shared-component-data
-
¥How to configure
{ "linter": { "rules": { "nursery": { "noVueDataObjectDeclaration": "error" } } }}¥Description
强制要求 Vue 组件的 data 选项声明为函数。
¥Enforce that Vue component data options are declared as functions.
在 Vue 3+ 中,将 data 定义为对象已被弃用,因为它会导致组件实例之间共享可变状态。此规则标记 data: { … } 的使用,并提供自动修复程序,将其转换为返回该对象的函数。
¥In Vue 3+, defining data as an object is deprecated because it leads to shared mutable state across component instances.
This rule flags usages of data: { … } and offers an automatic fix to convert it into a function returning that object.
另请参阅:– Vue 迁移指南 – 数据选项:https://v3-migration.vuejs.org/breaking-changes/data-option.html :contentReference[oaicite:0]{index="0"} – ESLint Vue 插件:no-deprecated-data-object-declaration:https://eslint-vue.nodejs.cn/rules/no-deprecated-data-object-declaration :contentReference[oaicite:1]{index="1"}
¥See also:
– Vue Migration Guide – Data Option: https://v3-migration.vuejs.org/breaking-changes/data-option.html :contentReference[oaicite:0]{index="0"}
– ESLint Plugin Vue: no-deprecated-data-object-declaration: https://eslint-vue.nodejs.cn/rules/no-deprecated-data-object-declaration :contentReference[oaicite:1]{index="1"}
¥Examples
¥Invalid
// component-local data via functionexport default { /* ✗ BAD */ data: { foo: null },};// Composition API helper also deprecateddefineComponent({ /* ✗ BAD */ data: { message: 'hi' }});// Vue 3 entrypoint via createAppcreateApp({ /* ✗ BAD */ data: { active: true }}).mount('#app');¥Valid
// component-local data via functionexport default { /* ✓ GOOD */ data() { return { foo: null }; }};// global registration with function syntaxVue.component('my-comp', { /* ✓ GOOD */ data: function () { return { count: 0 }; }});// Composition API and createApp entrypointsdefineComponent({ /* ✓ GOOD */ data() { return { message: 'hi' }; }});
createApp({ /* ✓ GOOD */ data: function() { return { active: true }; }}).mount('#app');¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号