app.component()是一个全局api,它可以用于注册全局组件或局部组件,是一个命名约定,通常用于表示应用程序的主要组件或根组件
全局组件的注册:
在main.ts中引入组件,并通过component('btn',Button)创建全局组件
import Button from './component/Button.vue'
createApp(App).component('btn',Button).mount('#app')
局部组件的注册:
直接在对应的文件中引入,然后使用就可以了
例如在vue2中
import Button from './component/Button.vue'
export default{
components:{ Button}
}
在vue3中的话直接引入就好了
也可以使用app.component来注册一个局部组件例如:
app.component('组件名称','组件')这个方法在特定的组件中使用,用于注册只在该组件及其子组件中可以使用的组件
app.component('Button',{template:'hhh'})