noVueDataObjectDeclaration
Summary
Section titled “Summary”- Rule available since:
v2.1.4
- Diagnostic Category:
lint/nursery/noVueDataObjectDeclaration
- This rule has a safe fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
- Inspired from
vue/no-deprecated-data-object-declaration
- Inspired from
vue/no-shared-component-data
- Inspired from
Description
Section titled “Description”Enforce that Vue component data
options are declared as functions.
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.
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.vuejs.org/rules/no-deprecated-data-object-declaration :contentReference[oaicite:1]{index="1"}
Examples
Section titled “Examples”Invalid
Section titled “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');
// 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');
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noVueDataObjectDeclaration": "error" } } }}