|
| 1 | +--- |
| 2 | +pageClass: rule-details |
| 3 | +sidebarDepth: 0 |
| 4 | +title: vue/no-undef-components |
| 5 | +description: disallow use of undefined components in `<template>` |
| 6 | +--- |
| 7 | +# vue/no-undef-components |
| 8 | + |
| 9 | +> disallow use of undefined components in `<template>` |
| 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 | +This rule warns reports component that are used in the `<template>`, but that are not defined in the `<script setup>` or the Options API's `components` section. |
| 14 | + |
| 15 | +Undefined components will be resolved from globally registered components. However, if you are not using global components, you can use this rule to prevent run-time errors. |
| 16 | + |
| 17 | +::: warning Note |
| 18 | +This rule cannot check globally registered components and components registered in mixins |
| 19 | +unless you add them as part of the ignored patterns. |
| 20 | +::: |
| 21 | + |
| 22 | +<eslint-code-block :rules="{'vue/no-undef-components': ['error']}"> |
| 23 | + |
| 24 | +```vue |
| 25 | +<script setup> |
| 26 | +import Foo from './Foo.vue' |
| 27 | +</script> |
| 28 | +
|
| 29 | +<template> |
| 30 | + <!-- ✓ GOOD --> |
| 31 | + <Foo /> |
| 32 | +
|
| 33 | + <!-- ✗ BAD --> |
| 34 | + <Bar /> |
| 35 | +</template> |
| 36 | +``` |
| 37 | + |
| 38 | +</eslint-code-block> |
| 39 | + |
| 40 | +<eslint-code-block :rules="{'vue/no-undef-components': ['error']}"> |
| 41 | + |
| 42 | +```vue |
| 43 | +<!-- ✓ GOOD --> |
| 44 | +<template> |
| 45 | + <div> |
| 46 | + <h2>Lorem ipsum</h2> |
| 47 | + <the-modal> |
| 48 | + <component is="TheInput" /> |
| 49 | + <component :is="'TheDropdown'" /> |
| 50 | + <TheButton>CTA</TheButton> |
| 51 | + </the-modal> |
| 52 | + </div> |
| 53 | +</template> |
| 54 | +
|
| 55 | +<script> |
| 56 | +import TheButton from 'components/TheButton.vue' |
| 57 | +import TheModal from 'components/TheModal.vue' |
| 58 | +import TheInput from 'components/TheInput.vue' |
| 59 | +import TheDropdown from 'components/TheDropdown.vue' |
| 60 | +
|
| 61 | +export default { |
| 62 | + components: { |
| 63 | + TheButton, |
| 64 | + TheModal, |
| 65 | + TheInput, |
| 66 | + TheDropdown, |
| 67 | + } |
| 68 | +} |
| 69 | +</script> |
| 70 | +``` |
| 71 | + |
| 72 | +</eslint-code-block> |
| 73 | + |
| 74 | +<eslint-code-block :rules="{'vue/no-undef-components': ['error']}"> |
| 75 | + |
| 76 | +```vue |
| 77 | +<!-- ✗ BAD --> |
| 78 | +<template> |
| 79 | + <div> |
| 80 | + <h2>Lorem ipsum</h2> |
| 81 | + <TheModal /> |
| 82 | + </div> |
| 83 | +</template> |
| 84 | +
|
| 85 | +<script> |
| 86 | +export default { |
| 87 | + components: { |
| 88 | +
|
| 89 | + } |
| 90 | +} |
| 91 | +</script> |
| 92 | +``` |
| 93 | + |
| 94 | +</eslint-code-block> |
| 95 | + |
| 96 | +## :wrench: Options |
| 97 | + |
| 98 | +```json |
| 99 | +{ |
| 100 | + "vue/no-undef-components": ["error", { |
| 101 | + "ignorePatterns": [] |
| 102 | + }] |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +- `ignorePatterns` Suppresses all errors if component name matches one or more patterns. |
| 107 | + |
| 108 | +### `ignorePatterns: ['custom(\\-\\w+)+']` |
| 109 | + |
| 110 | +<eslint-code-block :rules="{'vue/no-undef-components': ['error', { 'ignorePatterns': ['custom(\\-\\w+)+'] }]}"> |
| 111 | + |
| 112 | +```vue |
| 113 | +<script setup> |
| 114 | +</script> |
| 115 | +
|
| 116 | +<template> |
| 117 | + <!-- ✓ GOOD --> |
| 118 | + <CustomComponent /> |
| 119 | +
|
| 120 | + <!-- ✗ BAD --> |
| 121 | + <Bar /> |
| 122 | +</template> |
| 123 | +``` |
| 124 | + |
| 125 | +</eslint-code-block> |
| 126 | + |
| 127 | +<eslint-code-block :rules="{'vue/no-undef-components': ['error', { 'ignorePatterns': ['custom(\\-\\w+)+'] }]}"> |
| 128 | + |
| 129 | +```vue |
| 130 | +<!-- ✓ GOOD --> |
| 131 | +<template> |
| 132 | + <div> |
| 133 | + <h2>Lorem ipsum</h2> |
| 134 | + <CustomComponent /> |
| 135 | + </div> |
| 136 | +</template> |
| 137 | +
|
| 138 | +<script> |
| 139 | + export default { |
| 140 | + components: { |
| 141 | +
|
| 142 | + }, |
| 143 | + } |
| 144 | +</script> |
| 145 | +``` |
| 146 | + |
| 147 | +</eslint-code-block> |
| 148 | + |
| 149 | +<eslint-code-block :rules="{'vue/no-undef-components': ['error', { 'ignorePatterns': ['custom(\\-\\w+)+'] }]}"> |
| 150 | + |
| 151 | +```vue |
| 152 | +<!-- ✗ BAD --> |
| 153 | +<template> |
| 154 | + <div> |
| 155 | + <h2>Lorem ipsum</h2> |
| 156 | + <WarmButton /> |
| 157 | + </div> |
| 158 | +</template> |
| 159 | +
|
| 160 | +<script> |
| 161 | + export default { |
| 162 | + components: { |
| 163 | +
|
| 164 | + }, |
| 165 | + } |
| 166 | +</script> |
| 167 | +``` |
| 168 | + |
| 169 | +</eslint-code-block> |
| 170 | + |
| 171 | +## :couple: Related Rules |
| 172 | + |
| 173 | +- [vue/no-undef-properties](./no-undef-properties.md) |
| 174 | + |
| 175 | +## :mag: Implementation |
| 176 | + |
| 177 | +- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-undef-components.js) |
| 178 | +- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-undef-components.js) |
0 commit comments