import { createApp } from 'vue'
import App from './App.vue'
import router from './router/index'
import { store, key } from '@/store/index'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
// 统一导入el-icon图标
import * as Icons from '@element-plus/icons'
const app= createApp(App);
app.use(router).use(store,key).use(ElementPlus).mount('#app')
// 方式一
Object.keys(Icons).forEach((key) => {
app.component(key,Icons[key as keyof typeof Icons])
});
使用方式:
<component :is="xxx" class="xxx">
方式二
const Icon = (props: { icon: string }) => {
const { icon } = props;
return createVNode(Icons[icon as keyof typeof Icons]);
};
app.component('Icon', Icon);
使用方式:
<Icon icon="xxx">
解决type ‘string’ can’t be used to index type 'typeof 字符串不能做下标的错,在tsconfig.json的 compilerOptions 中添加如下配置
方式一:
"suppressImplicitAnyIndexErrors": true, //解决用字符串做下标报错
方式二:
key as keyof typeof Icons