23.组件注册方式

组件注册方式

image-20220827160957592

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

全局注册

import { createApp } from 'vue'
import App from './App.vue'
import GlobalComponent from "./components/GlobalComponent.vue"


const app = createApp(App);
app.component("GlobalComponent",GlobalComponent)
app.mount('#app');
<template>
  <h3>全局应用组件</h3>
</template>

局部注册

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

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

局部注册需要使用 components 选项

<template>
 <GlobalComponent />
</template>
<script>
import GlobalComponent from "./components/GlobalComponent.vue"
export default {
 components:{
  GlobalComponent
  }
}
</script>
<style>
*{
 margin: 0;
 padding: 0;
}
</style>
<style>
*{
 margin: 0;
 padding: 0;
}
</style>

相关推荐

  1. 学习Vue 02-25 注册全局组件

    2024-04-22 00:30:05       54 阅读
  2. vue3组件注册

    2024-04-22 00:30:05       59 阅读
  3. vue3组件注册

    2024-04-22 00:30:05       45 阅读
  4. vue3基础: 组件注册

    2024-04-22 00:30:05       56 阅读
  5. Vue 批量注册全局组件

    2024-04-22 00:30:05       56 阅读
  6. vue3注册全局组件

    2024-04-22 00:30:05       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-22 00:30:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-22 00:30:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-22 00:30:05       87 阅读
  4. Python语言-面向对象

    2024-04-22 00:30:05       96 阅读

热门阅读

  1. k8s中修复mongodb启动失败

    2024-04-22 00:30:05       23 阅读
  2. Neural Radiance Fields (NeRF) 和 3D Gaussian Splatting区别

    2024-04-22 00:30:05       34 阅读
  3. 展开说说:Android Fragment完全解析-卷二

    2024-04-22 00:30:05       39 阅读
  4. vue大屏

    2024-04-22 00:30:05       32 阅读
  5. jQuery 选择器有几种,分别是什么

    2024-04-22 00:30:05       34 阅读
  6. Linux系统的账号和权限管理

    2024-04-22 00:30:05       36 阅读
  7. 赠品:跳动的心

    2024-04-22 00:30:05       36 阅读
  8. ZYNQ-700呼吸灯

    2024-04-22 00:30:05       36 阅读
  9. 【设计模式】8、adapter 适配器模式

    2024-04-22 00:30:05       32 阅读
  10. 考古:MFC界面的自适应缩放(代码示例)

    2024-04-22 00:30:05       37 阅读