Vue3中全局注册组件,并统一处理不用一一注册
![图片[1]-Vue3注册全局组件-朝晞小屋](http://tu.zxiyun.com/local/2023/10/05/651ea97953aef.jpg)
1.在src下components中创建index.ts
// 在index.ts中统一注册全局组件
import type { Component } from 'vue';
import HelloWorld from '@/components/HelloWorld.vue';
import Hello from '@/components/Hello.vue'
import World from '@/components/world.vue'
// 如果使用的是 JS 可以删除类型校验
const components: {
[propName: string]: Component;
} = {
HelloWorld,
Hello,
World
};
export default components;
2.在main.ts中添加如下代码
// 在此定义组件
import globalComponent from '@/components/index';
// 注册全局的组件
for (const componentItme in globalComponent) {
app.component(componentItme, globalComponent[componentItme]);
}
3.使用,在任意vue页面直接调用注册的组件名
<template>
<div>
<HelloWorld ref="helloworld" @open="open"></HelloWorld>
</div>
</template>
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END

















![战神引擎传奇手游【1.80恒世复古[白猪3.1]】2025整理复古服务端+冰雪之城+水上城市+迷幻蚁园【站长亲测】-朝晞小屋](https://aliyun.zxiyun.com/images/2025/11/62184-战神引擎传奇手游【1-80恒世复古白猪3-1】2025整理复古-j3t3k5-800x450.jpg)


暂无评论内容