本文介绍: ① 在src/components文件夹下创建index.ts:用于注册components文件夹下全部的全局组件。②在入口文件(通常是main.ts)引入步骤①的index.ts文件,通过app.use方法安装自定义插件。
① 在src/components文件夹下创建SvgIcon组件
<template>
<div>
<svg :style="{ width: width, height: height }">
<use :xlink:href="prefix + name" :fill="color"></use>
</svg>
</div>
</template>
<script setup lang="ts">
defineProps({
// xlink:href属性值的前缀
prefix: {
type: String,
default: '#icon-'
},
// svg矢量图名字
name: String,
// svg图标颜色
color: {
type: String,
default: ''
},
// svg图标宽度
width: {
type: String,
default: '16px'
},
// svg图标高度
height: {
type: String,
default: '16px'
}
})
</script>
<style scoped></style>
② 在src/components文件夹下创建index.ts:用于注册components文件夹下全部的全局组件
// 引入项目中的全部全局组件
import SvgIcon from './SvgIcon/index.vue'
import Pagination from './Pagination/index.vue'
// 定义全局组件对象:用于存放所有要引入的全局组件
const allGlobalComponents: any = {
SvgIcon,
Pagination
}
// 对外暴露一个插件对象
export default {
install(app: any) {
// 遍历注册全局组件对象中的所有组件
Object.keys(allGlobalComponents).forEach(keys => {
app.component(keys, allGlobalComponents[keys])
})
}
}
③在入口文件(通常是main.ts)引入步骤②的index.ts文件,通过app.use方法安装自定义插件
// 引入自定义插件对象:注册全局组件
import globalComponent from '@/components/index.ts'
// 安装自定义插件,使用该方法就会调用自定义插件对象文件中的install方法,从而进行全局组件注册
app.use(globalComponent)
原文地址:https://blog.csdn.net/m0_67992720/article/details/134673608
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_7441.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。