noVueArrowFuncInWatch
Цей контент ще не доступний вашою мовою.
Summary
Section titled “Summary”- Rule available since:
v2.3.14 - Diagnostic Category:
lint/nursery/noVueArrowFuncInWatch - This rule has an unsafe fix.
- The default severity of this rule is information.
- This rule belongs to the following domains:
- Sources:
- Same as
vue/no-arrow-functions-in-watch
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noVueArrowFuncInWatch": "error" } } }}Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”<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:
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.