vue3 组件注册
全局注册
所有子组件可以使用全局注册的组件。
使用 vue 应用实例的 .component() 方法:
1 | import { createApp } from 'vue'; |
对于单文件组件:
1 | import MyComponent from './App.vue' |
链式:
1 | app. |
局部注册
优点
- tree-shaking:全局注册的组件,即使不用,也会出现在打包后的 JS 文件中;而局部注册会移除未使用的组件。
- 依赖关系明确:全局注册使大型项目中组件的依赖关系不够明确;而局部组件使组件间的依赖关系更加明确。
- Title: vue3 组件注册
- Author: Gabrielle
- Created at : 2023-12-13 15:16:29
- Updated at : 2025-05-20 10:47:28
- Link: https://zoella-w.github.io/2023/12/13/15-vue3组件注册/
- License: This work is licensed under CC BY-NC-SA 4.0.