|
| 1 | +--- |
| 2 | +pageClass: rule-details |
| 3 | +sidebarDepth: 0 |
| 4 | +title: vue/no-undef-properties |
| 5 | +description: disallow undefined properties |
| 6 | +--- |
| 7 | +# vue/no-undef-properties |
| 8 | + |
| 9 | +> disallow undefined properties |
| 10 | +
|
| 11 | +- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> |
| 12 | + |
| 13 | +## :book: Rule Details |
| 14 | + |
| 15 | +This rule warns of using undefined properties. |
| 16 | +This rule can help you locate potential errors resulting from misspellings property names, and implicitly added properties. |
| 17 | + |
| 18 | +::: warning Note |
| 19 | +This rule cannot detect properties defined in other files or components. |
| 20 | +Note that there are many false positives if you are using mixins. |
| 21 | +::: |
| 22 | + |
| 23 | +<eslint-code-block :rules="{'vue/no-undef-properties': ['error']}"> |
| 24 | + |
| 25 | +```vue |
| 26 | +<template> |
| 27 | + <!-- ✓ GOOD --> |
| 28 | + <div>{{ name }}: {{ count }}</div> |
| 29 | + <!-- ✗ BAD --> |
| 30 | + <div>{{ label }}: {{ cnt }}</div> |
| 31 | +</template> |
| 32 | +<script setup> |
| 33 | +const prop = defineProps(['name', 'def']) |
| 34 | +let count = 0 |
| 35 | +
|
| 36 | +/* ✓ GOOD */ |
| 37 | +watch(() => prop.def, () => console.log('Updated!')) |
| 38 | +
|
| 39 | +/* ✗ BAD */ |
| 40 | +watch(() => prop.undef, () => console.log('Updated!')) |
| 41 | +</script> |
| 42 | +``` |
| 43 | + |
| 44 | +</eslint-code-block> |
| 45 | + |
| 46 | +<eslint-code-block :rules="{'vue/no-undef-properties': ['error']}"> |
| 47 | + |
| 48 | +```vue |
| 49 | +<template> |
| 50 | + <!-- ✓ GOOD --> |
| 51 | + <div>{{ name }}: {{ count }}</div> |
| 52 | + <!-- ✗ BAD --> |
| 53 | + <div>{{ label }}: {{ cnt }}</div> |
| 54 | +</template> |
| 55 | +<script> |
| 56 | + export default { |
| 57 | + props: ['name'], |
| 58 | + data () { |
| 59 | + return { |
| 60 | + count: 0 |
| 61 | + } |
| 62 | + }, |
| 63 | + methods: { |
| 64 | + click() { |
| 65 | + /* ✓ GOOD */ |
| 66 | + this.count++ |
| 67 | +
|
| 68 | + /* ✗ BAD */ |
| 69 | + this.cnt++ |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +</script> |
| 74 | +``` |
| 75 | + |
| 76 | +</eslint-code-block> |
| 77 | + |
| 78 | +## :wrench: Options |
| 79 | + |
| 80 | +```json |
| 81 | +{ |
| 82 | + "vue/no-undef-properties": ["error", { |
| 83 | + "ignores": ["/^\\$/"] |
| 84 | + }] |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +- `ignores` (`string[]`) ... An array of property names or patterns that have already been defined property, or property to ignore from the check. Default is `["/^\\$/"]`. |
| 89 | + |
| 90 | +### `"ignores": ["/^\\$/"]` (default) |
| 91 | + |
| 92 | +<eslint-code-block :rules="{'vue/no-undef-properties': ['error', {ignores: ['/^\\$/']}]}"> |
| 93 | + |
| 94 | +```vue |
| 95 | +<template> |
| 96 | + <!-- ✓ GOOD --> |
| 97 | + <div>{{ $t('foo') }}</div> |
| 98 | +</template> |
| 99 | +<script> |
| 100 | + export default { |
| 101 | + mounted() { |
| 102 | + /* ✓ GOOD */ |
| 103 | + const hash = this.$route.hash |
| 104 | + } |
| 105 | + } |
| 106 | +</script> |
| 107 | +``` |
| 108 | + |
| 109 | +</eslint-code-block> |
| 110 | + |
| 111 | +## :mag: Implementation |
| 112 | + |
| 113 | +- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-undef-properties.js) |
| 114 | +- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-undef-properties.js) |
0 commit comments