Aller au contenu

noVueArrowFuncInWatch

Ce contenu n’est pas encore disponible dans votre langue.

biome.json
{
"linter": {
"rules": {
"nursery": {
"noVueArrowFuncInWatch": "error"
}
}
}
}

Disallows using arrow functions when defining a watcher.

When using the Options API in Vue.js, defining watchers with arrow functions is discouraged. This is because arrow functions bind to their parent context, which means that the this keyword inside the arrow function does not refer to the Vue instance as expected. Instead, it refers to the context in which the arrow function was defined, which can be confusing.

<script>
export default {
watch: {
foo: (val, oldVal) => {
console.log('new: %s, old: %s', val, oldVal)
}
}
}
</script>
code-block.vue:3:10 lint/nursery/noVueArrowFuncInWatch  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Found arrow function in Vue watcher definition.

1 │ export default {
2 │ watch: {
> 3 │ foo: (val, oldVal) => {
^^^^^^^^^^^^^^^^^^
> 4 │ console.log(‘new: %s, old: %s’, val, oldVal)
> 5 │ }
^
6 │ }
7 │ }

Using an arrow function here means that the `this` context will not refer to the Vue instance, which is probably not what you want.

This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

Unsafe fix: Rewrite the arrow function into a function expression.

1 1 export default {
2 2 watch: {
3 - ····foo:·(val,·oldVal)·=>·{
3+ ····foo:·function(val,·oldVal)·{
4 4 console.log(‘new: %s, old: %s’, val, oldVal)
5 5 }

<script>
export default {
watch: {
foo: {
handler: (val, oldVal) => {
console.log('new: %s, old: %s', val, oldVal)
}
}
}
}
</script>
code-block.vue:4:16 lint/nursery/noVueArrowFuncInWatch  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Found arrow function in Vue watcher definition.

2 │ watch: {
3 │ foo: {
> 4 │ handler: (val, oldVal) => {
^^^^^^^^^^^^^^^^^^
> 5 │ console.log(‘new: %s, old: %s’, val, oldVal)
> 6 │ }
^
7 │ }
8 │ }

Using an arrow function here means that the `this` context will not refer to the Vue instance, which is probably not what you want.

This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

Unsafe fix: Rewrite the arrow function into a function expression.

2 2 watch: {
3 3 foo: {
4 - ······handler:·(val,·oldVal)·=>·{
4+ ······handler:·function(val,·oldVal)·{
5 5 console.log(‘new: %s, old: %s’, val, oldVal)
6 6 }

<script>
export default {
watch: {
a: function (val, oldVal) {
console.log('new: %s, old: %s', val, oldVal)
},
b: 'someMethod',
c: {
handler: function (val, oldVal) { /* ... */ },
deep: true
},
d: {
handler: 'someMethod',
immediate: true
},
e: [
'handle1',
function handle2 (val, oldVal) { /* ... */ },
{
handler: function handle3 (val, oldVal) { /* ... */ },
/* ... */
}
],
'e.f': function (val, oldVal) { /* ... */ }
}
}
</script>

References: