vue3 组件注册

Zoella Lv3

全局注册

所有子组件可以使用全局注册的组件。

使用 vue 应用实例的 .component() 方法:

1
2
3
4
5
6
import { createApp } from 'vue';
const app = createApp({});

app.component('myComponent',{
// 组件的实现
});

对于单文件组件:

1
2
3
import MyComponent from './App.vue'

app.component('MyComponent', MyComponent);

链式:

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

局部注册

优点

  1. tree-shaking:全局注册的组件,即使不用,也会出现在打包后的 JS 文件中;而局部注册会移除未使用的组件。
  2. 依赖关系明确:全局注册使大型项目中组件的依赖关系不够明确;而局部组件使组件间的依赖关系更加明确。
  • Title: vue3 组件注册
  • Author: Zoella
  • Created at : 2023-12-13 15:16:29
  • Updated at : 2023-12-26 11:20:46
  • Link: https://zoella-w.github.io/2023/12/13/15-vue3组件注册/
  • License: This work is licensed under CC BY-NC-SA 4.0.
On this page
vue3 组件注册