原文 👉 组件注册 | Vue.js

此章节假设你已经看过了组件基础。若你还不了解组件是什么,请先阅读该章节。

一个 Vue 组件在使用前需要先被“注册”,这样 Vue 才能在渲染模板时找到其对应的实现。组件注册有两种方式:

  1. 全局注册
  2. 局部注册

全局注册

我们可以使用 Vue 应用实例 的 app.component() 方法,让组件在当前 Vue 应用中全局可用。

  1. import { createApp } from 'vue'
  2. const app = createApp({})
  3. app.component(
  4. // 注册的名字
  5. 'MyComponent',
  6. // 组件的实现
  7. {
  8. /* ... */
  9. }
  10. )

如果使用单文件组件,你可以注册被导入的 .vue 文件:

  1. import MyComponent from './App.vue'
  2. app.component('MyComponent', MyComponent)

app.component() 方法可以被链式调用

  1. app
  2. .component('ComponentA', ComponentA)
  3. .component('ComponentB', ComponentB)
  4. .component('ComponentC', ComponentC)

全局注册的组件可以在此应用的任意组件的模板中使用

  1. <!-- 这在当前应用的任意组件中都可用 -->
  2. <ComponentA/>
  3. <ComponentB/>
  4. <ComponentC/>

所有的子组件也可以使用全局注册的组件,这意味着这三个组件也都可以在彼此内部使用(组件递归)

模板预编译问题

问题分析

  1. import { createApp, defineComponent, ref } from 'vue'
  2. import './style.css'
  3. import A from './A.vue'
  4. const app = createApp(A)
  5. app.component(
  6. 'MyComp',
  7. defineComponent({
  8. setup() {
  9. const counter = ref(0)
  10. const increase = () => {
  11. counter.value++
  12. }
  13. return { counter, increase }
  14. },
  15. template: `
  16. <div>
  17. <p>Counter: {{ counter }}</p>
  18. <button @click="increase">Increase</button>
  19. </div>
  20. `
  21. })
  22. )
  23. app.mount('#app')
  1. <template>
  2. <MyComp />
  3. </template>

此时页面中什么都不会显示,并且会在调试工具中提示我们 template 无法编译:
image.png

解决方案1 手写 render 函数 不推荐

  1. import { createApp, defineComponent, h, ref } from 'vue'
  2. import './style.css'
  3. import A from './A.vue'
  4. const app = createApp(A)
  5. app.component(
  6. 'MyComp',
  7. defineComponent({
  8. setup() {
  9. const counter = ref(0)
  10. const increase = () => {
  11. counter.value++
  12. }
  13. return { counter, increase }
  14. },
  15. render() {
  16. return h('div', {}, [
  17. h('p', `Counter: ${this.counter}`),
  18. h('button', { onClick: this.increase }, 'Increase')
  19. ])
  20. }
  21. })
  22. )
  23. app.mount('#app')

image.png

解决方案1:手写 render 函数,不使用 template 模板。

这种做法相当于直接手写模板初步编译的产物 render 函数,这么做相当于直接无视 vue 提供的模板编译预编译步骤,但是这种做法并不推荐。

解决方案2 单文件组件 推荐

虽然方案1确实可行,不过手写 render 函数看起来并不比直接写 template 来的直观,所以更推荐的做法是将 MyComp 组件的内容封装到一个 .vue 文件(单文件组件)中。

  1. import { createApp } from 'vue'
  2. import './style.css'
  3. import A from './A.vue'
  4. import MyComp from './MyComp.vue' // 引入 MyComp.vue 文件
  5. const app = createApp(A)
  6. app.component('MyComp', MyComp) // 注册 MyComp 组件
  7. app.mount('#app')
  1. <template>
  2. <div>
  3. <p>Counter: {{ counter }}</p>
  4. <button @click="increase">Increase</button>
  5. </div>
  6. </template>
  7. <script setup>
  8. import {ref} from "vue"
  9. const counter = ref(0)
  10. const increase = () => {
  11. counter.value++
  12. }
  13. </script>

image.png

局部注册

全局注册的问题
全局注册虽然很方便,但有以下几个问题:

  1. 全局注册,但并没有被使用的组件无法在生产打包时被自动移除 (也叫“tree-shaking”)。如果你全局注册了一个组件,即使它并没有被实际使用,它仍然会出现在打包后的 JS 文件中。
  2. 全局注册在大型项目中使项目的依赖关系变得不那么明确。在父组件中使用子组件时,不太容易定位子组件的实现。和使用过多的全局变量一样,这可能会影响应用长期的可维护性。

局部注册的优势
相比之下,局部注册的组件需要在使用它的父组件中显式导入,并且只能在该父组件中使用。它的优点是:

  1. 对 tree-shaking 更加友好
  2. 使组件之间的依赖关系更加明确
  1. <template>
  2. <h1>B.vue</h1>
  3. </template>

在使用 <script setup> 的单文件组件中,导入的组件可以直接在模板中使用,无需注册:

  1. <script setup>
  2. import B from "./B.vue"
  3. </script>
  4. <template>
  5. <B />
  6. </template>

如果没有使用 <script setup>,则需要使用 components 选项来显式注册:

  1. <script>
  2. import B from "./B.vue"
  3. export default {
  4. components: {
  5. B,
  6. }
  7. }
  8. </script>
  9. <template>
  10. <B />
  11. </template>

image.png

对于每个 components 对象里的属性,它们的 key 名就是注册的组件名,而值就是相应组件的实现。上面的例子中使用的是 ES2015 的缩写语法,等价于:

  1. export default {
  2. components: {
  3. B: B,
  4. // B
  5. }
  6. }

请注意:局部注册的组件在后代组件中并不可用。在这个例子中,B 注册后仅在当前组件 A 中可用,而在任何的子组件或更深层的子组件中都不可用。

组件名格式

大驼峰
在整个指引中,我们都使用 PascalCase 作为组件名的注册格式,这是因为:

  1. PascalCase 是合法的 JavaScript 标识符。这使得在 JavaScript 中导入和注册组件都很容易,同时 IDE 也能提供较好的自动补全
  2. **<PascalCase />** 在模板中更明显地表明了这是一个 Vue 组件,而不是原生 HTML 元素。同时也能够将 Vue 组件和自定义元素 (web components) 区分开来。

在单文件组件和内联字符串模板中,我们都推荐这样做。但是,PascalCase 的标签名在 DOM 模板中是不可用的,详情参见 DOM 模板解析注意事项

为了方便,Vue 支持将模板中使用 kebab-case 的标签解析为使用 PascalCase 注册的组件。这意味着一个以 MyComponent 为名注册的组件,在模板中可以通过 引用。这让我们能够使用同样的 JavaScript 组件注册代码来配合不同来源的模板。

  1. <template>
  2. <h1>BComp.vue</h1>
  3. </template>
  1. <script>
  2. import BComp from "./BComp.vue"
  3. export default {
  4. components: {
  5. BComp,
  6. }
  7. }
  8. </script>
  9. <template>
  10. <BComp />
  11. <b-Comp />
  12. <b-comp />
  13. <B-comp />
  14. </template>

image.png

组件名推荐写法
在 vue 中,上述这些写法都是可以识别的,不过按照官方文档,推荐使用下面这两种写法:

  1. PascalCase 对应上述写法:<BComp />
  2. kebab-case 对应上述写法:<b-comp />