全部学科
Python全栈
python
NodeJS全栈
nodejs
📅 2026-05-20 5 分钟 ✍️ juanwangdev

组件注册

组件必须先注册才能被使用。

全局注册

JavaScript
import { createApp } from 'vue'
import App from './App.vue'
import MyComponent from './components/MyComponent.vue'

const app = createApp(App)
app.component('MyComponent', MyComponent)
app.mount('#app')

全局注册的组件可在任何组件的模板中使用。

局部注册

JavaScript
import MyComponent from './components/MyComponent.vue'

export default {
  components: {
    MyComponent
  }
}

局部注册的组件仅在当前组件内可用。

对比

特性全局注册局部注册
使用范围全局当前组件
打包体积可能被全部打包按需加载
适用场景基础组件(按钮、输入框)业务组件

推荐使用局部注册,有利于代码分割和按需加载。

要点总结

  • 全局注册使用 app.component()
  • 局部注册在 components 选项中声明
  • 局部注册更利于性能优化和代码分割
  • 基础 UI 组件适合全局注册
想在手机上练习这篇文章的配套题目?
使用微信卷王开发者小程序,打开首页顶部扫码功能识别二维码
← 上一篇 组件基本概念
下一篇 → 组件模板
扫码体验小程序
加载中
想在手机上刷题学习?
使用微信卷王开发者小程序,打开首页顶部扫码功能识别二维码