|
| 1 | +--- |
| 2 | +title: 组件注册 |
| 3 | +type: guide |
| 4 | +order: 101 |
| 5 | +--- |
| 6 | + |
| 7 | +> 该页面假设你已经阅读过了[组件基础](components.html)。如果你还对组件不太了解,推荐你先阅读它。 |
| 8 | +
|
| 9 | +## 组件名 |
| 10 | + |
| 11 | +在注册一个组件的时候,我们始终需要给它一个名字。比如在全局注册的时候我们已经看到了: |
| 12 | + |
| 13 | +```js |
| 14 | +Vue.component('my-component-name', { /* ... */ }) |
| 15 | +``` |
| 16 | + |
| 17 | +该组件名就是 `Vue.component` 的第一个参数。 |
| 18 | + |
| 19 | +你给予组件的名字可能依赖于你打算拿它来做什么。当直接在 DOM 中使用一个组件 (而不是在字符串模板或[单文件组件](single-file-components.html)) 的时候,我们强烈推荐遵循 [W3C 规范](https://www.w3.org/TR/custom-elements/#concepts)中的自定义组件名 (字母全小写且必须包含一个连字符)。这会帮助你避免和当前以及未来的 HTML 元素相冲突。 |
| 20 | + |
| 21 | +你可以在[风格指南](../style-guide/#基础组件名-强烈推荐)中查阅到关于组件名的其它建议。 |
| 22 | + |
| 23 | +### 组件名大小写 |
| 24 | + |
| 25 | +定义组件名的方式有两种: |
| 26 | + |
| 27 | +#### 使用 kebab-case |
| 28 | + |
| 29 | +```js |
| 30 | +Vue.component('my-component-name', { /* ... */ }) |
| 31 | +``` |
| 32 | + |
| 33 | +当使用 kebab-case (短横线分隔命名) 定义一个组件时,你也必须在引用这个自定义元素时使用 kebab-case,例如 `<my-component-name>`。 |
| 34 | + |
| 35 | +#### 使用 PascalCase |
| 36 | + |
| 37 | +```js |
| 38 | +Vue.component('MyComponentName', { /* ... */ }) |
| 39 | +``` |
| 40 | + |
| 41 | +当使用 PascalCase (驼峰式命名) 定义一个组件时,你在引用这个自定义元素时两种命名法都可以使用。也就是说 `<my-component-name>` 和 `<MyComponentName>` 都是可接受的。注意,尽管如此,直接在 DOM (即非字符串的模板) 中使用时只有 kebab-case 是有效的。 |
| 42 | + |
| 43 | +## 全局注册 |
| 44 | + |
| 45 | +到目前为止,我们只用过 `Vue.component` 来创建组件: |
| 46 | + |
| 47 | +```js |
| 48 | +Vue.component('my-component-name', { |
| 49 | + // ... 选项 ... |
| 50 | +}) |
| 51 | +``` |
| 52 | + |
| 53 | +这些组件是**全局注册的**。也就是说它们在注册之后可以用在任何新创建的 Vue 根实例 (`new Vue`) 的模板中。比如: |
| 54 | + |
| 55 | +```js |
| 56 | +Vue.component('component-a', { /* ... */ }) |
| 57 | +Vue.component('component-b', { /* ... */ }) |
| 58 | +Vue.component('component-c', { /* ... */ }) |
| 59 | + |
| 60 | +new Vue({ el: '#app' }) |
| 61 | +``` |
| 62 | + |
| 63 | +```html |
| 64 | +<div id="app"> |
| 65 | + <component-a></component-a> |
| 66 | + <component-b></component-b> |
| 67 | + <component-c></component-c> |
| 68 | +</div> |
| 69 | +``` |
| 70 | + |
| 71 | +在所有子组件中也是如此,也就是说这三个组件_在各自内部_也都可以相互使用。 |
| 72 | + |
| 73 | +## 局部注册 |
| 74 | + |
| 75 | +全局注册往往是不够理想的。比如,如果你使用一个像 webpack 这样的构件系统,全局注册所有的组件意味着即便你已经不再使用一个组件了,它仍然会被包含在你最终的构建结果中。这造成了用户下载的 JavaScript 的无谓的增加。 |
| 76 | + |
| 77 | +在这些情况下,你可以通过一个普通的 JavaScript 对象来定义组件: |
| 78 | + |
| 79 | +```js |
| 80 | +var ComponentA = { /* ... */ } |
| 81 | +var ComponentB = { /* ... */ } |
| 82 | +var ComponentC = { /* ... */ } |
| 83 | +``` |
| 84 | + |
| 85 | +然后在 `components` 选项中定义你想要使用的组件: |
| 86 | + |
| 87 | +```js |
| 88 | +new Vue({ |
| 89 | + el: '#app' |
| 90 | + components: { |
| 91 | + 'component-a': ComponentA, |
| 92 | + 'component-b': ComponentB |
| 93 | + } |
| 94 | +}) |
| 95 | +``` |
| 96 | + |
| 97 | +对于 `components` 对象中的每个属性来说,其属性名就是自定义元素的名字,其属性值就是这个组件的选项对象。 |
| 98 | + |
| 99 | +注意**局部注册的组件在其子组件中_不可用_**。例如,如果你希望 `ComponentA` 在 `ComponentB` 中可用,则你需要这样写: |
| 100 | + |
| 101 | +```js |
| 102 | +var ComponentA = { /* ... */ } |
| 103 | + |
| 104 | +var ComponentB = { |
| 105 | + components: { |
| 106 | + 'component-a': ComponentA |
| 107 | + }, |
| 108 | + // ... |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +或者如果你通过 Babel 和 webpack 使用 ES2015 模块,那么代码看起来更像: |
| 113 | + |
| 114 | +```js |
| 115 | +import ComponentA from './ComponentA.vue' |
| 116 | + |
| 117 | +export default { |
| 118 | + components: { |
| 119 | + ComponentA |
| 120 | + }, |
| 121 | + // ... |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +注意在 ES2015+ 中,在对象中放一个类似 `ComponentA` 的变量名其实是 `ComponentA: ComponentA` 的缩写,即这个变量名同时是: |
| 126 | + |
| 127 | +- 用在模板中的自定义元素的名称 |
| 128 | +- 包含了这个组件选项的变量名 |
| 129 | + |
| 130 | +## 模块系统 |
| 131 | + |
| 132 | +如果你没有通过 `import`/`require` 使用一个模块系统,也许可以暂且跳过这个章节。如果你使用了,那么我们会为你提供一些特殊的使用说明和注意事项。 |
| 133 | + |
| 134 | +### 在模块系统中局部注册 |
| 135 | + |
| 136 | +如果你还在阅读,说明你使用了诸如 Babel 和 webpack 的模块系统。在这些情况下,我们推荐在每个组件各自的文件里创建一个 `components`。 |
| 137 | + |
| 138 | +然后你需要在局部注册之前导入每个你想使用的组件。例如,在一个假设的 `ComponentB.js` 或 `ComponentB.vue` 文件中: |
| 139 | + |
| 140 | +```js |
| 141 | +import ComponentA from './ComponentA' |
| 142 | +import ComponentC from './ComponentC' |
| 143 | + |
| 144 | +export default { |
| 145 | + components: { |
| 146 | + ComponentA, |
| 147 | + ComponentC |
| 148 | + }, |
| 149 | + // ... |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +现在 `ComponentA` 和 `ComponentC` 都可以在 `ComponentB` 的模板中使用了。 |
| 154 | + |
| 155 | +### 基础组件的自动化全局注册 |
| 156 | + |
| 157 | +可能你的许多组件只是包裹了一个输入框或按钮之类的元素,是相对通用的。我们有时候会把它们称为[基础组件](../style-guide/#基础组件名-强烈推荐),它们会在各个组件中被频繁的用到。 |
| 158 | + |
| 159 | +所以会导致很多组件里都会有一个包含基础组件的长列表: |
| 160 | + |
| 161 | +```js |
| 162 | +import BaseButton from './BaseButton.vue' |
| 163 | +import BaseIcon from './BaseIcon.vue' |
| 164 | +import BaseInput from './BaseInput.vue' |
| 165 | + |
| 166 | +export default { |
| 167 | + components: { |
| 168 | + BaseButton, |
| 169 | + BaseIcon, |
| 170 | + BaseInput |
| 171 | + } |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +而只是用于模板中的一小部分: |
| 176 | + |
| 177 | +```html |
| 178 | +<BaseInput |
| 179 | + v-model="searchText" |
| 180 | + @keydown.enter="search" |
| 181 | +/> |
| 182 | +<BaseButton @click="search"> |
| 183 | + <BaseIcon name="search"/> |
| 184 | +</BaseButton> |
| 185 | +``` |
| 186 | + |
| 187 | +幸好如果你使用了 webpack (或在内部使用了 webpack 的 [Vue CLI 3+](https://github.com/vuejs/vue-cli)),那么就可以使用 `require.context` 只全局注册这些非常通用的基础组件。这里有一份可以让你在应用入口文件 (比如 `src/main.js`) 中全局导入基础组件的示例代码: |
| 188 | + |
| 189 | +```js |
| 190 | +import Vue from 'vue' |
| 191 | +import upperFirst from 'lodash/upperFirst' |
| 192 | +import camelCase from 'lodash/camelCase' |
| 193 | + |
| 194 | +const requireComponent = require.context( |
| 195 | + // 其组件目录的相对路径 |
| 196 | + './components', |
| 197 | + // 是否查询其子目录 |
| 198 | + false, |
| 199 | + // 匹配基础组件文件名的正则表达式 |
| 200 | + /Base[A-Z]\w+\.(vue|js)$/ |
| 201 | +) |
| 202 | + |
| 203 | +requireComponent.keys().forEach(fileName => { |
| 204 | + // 获取组件配置 |
| 205 | + const componentConfig = requireComponent(fileName) |
| 206 | + |
| 207 | + // 获取组件的 PascalCase 命名 |
| 208 | + const componentName = upperFirst( |
| 209 | + camelCase( |
| 210 | + // 剥去文件名开头的 `'./` 和结尾的扩展名 |
| 211 | + fileName.replace(/^\.\/(.*)\.\w+$/, '$1') |
| 212 | + ) |
| 213 | + ) |
| 214 | + |
| 215 | + // 全局注册组件 |
| 216 | + Vue.component( |
| 217 | + componentName, |
| 218 | + // 如果这个组件选项是通过 `export default` 导出的, |
| 219 | + // 那么就会优先使用 `.default`, |
| 220 | + // 否则回退到使用模块的根。 |
| 221 | + componentConfig.default || componentConfig |
| 222 | + ) |
| 223 | +}) |
| 224 | +``` |
| 225 | + |
| 226 | +记住**全局注册的行为必须在根 Vue 实例 (通过 `new Vue`) 创建之前发生**。[这里](https://github.com/chrisvfritz/vue-enterprise-boilerplate/blob/master/src/components/_globals.js)有一个真实项目情景下的示例。 |
0 commit comments