Skip to content

noVueDataObjectDeclaration

¥Summary

¥How to configure

biome.json
{
"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-declarationhttps://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 function
export default {
/* ✗ BAD */
data: { foo: null },
};
// Composition API helper also deprecated
defineComponent({
/* ✗ BAD */
data: { message: 'hi' }
});
// Vue 3 entrypoint via createApp
createApp({
/* ✗ BAD */
data: { active: true }
}).mount('#app');

¥Valid

// component-local data via function
export default {
/* ✓ GOOD */
data() {
return { foo: null };
}
};
// global registration with function syntax
Vue.component('my-comp', {
/* ✓ GOOD */
data: function () {
return { count: 0 };
}
});
// Composition API and createApp entrypoints
defineComponent({
/* ✓ GOOD */
data() {
return { message: 'hi' };
}
});
createApp({
/* ✓ GOOD */
data: function() {
return { active: true };
}
}).mount('#app');

¥Related links