vue3 批量注入elementUI icon组件以及解决用字符串做下标报错问题

问题1: vue3 批量注入elementUI icon组件

main.ts文件,as为icon起别名为ElIcons

import ElementPlus from "element-plus";
import * as ElIcons from '@element-plus/icons'
const app = createApp(App);
//批量注入icon组件
Object.keys(ElIcons).forEach(key =>{
    //console.log(ElIcons,key);
    app.component(key, ElIcons[key]);
})
app.use(ElementPlus).use(router).use(store).mount('#app');

问题2: 解决用字符串做下标报错问题,上述代码中 app.component(key, ElIcons[key]); key为字符串,如果不处理就会报错

报错信息: Element implicitly has an ‘any’ type because expression of type ‘string’ can’t be used to index type ‘typeof import("…/node_modules/@element-plus/icons/types/index")’. No index signature with a parameter of type ‘string’ was found on type ‘typeof import(“C:/…/node_modules/@element-plus/icons/types/index”)’.

解决方案:
在tsconfig.json中配置suppressImplicitAnyIndexErrors: true

{
    "compilerOptions": {
        "suppressImplicitAnyIndexErrors": true,
        ...
    },
    ...
}

版权声明:本文为qq_37656005原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。