按照Ant Design Vue 的示例中,有按需加載組件,正常的組件可以正常使用,然而當使用ICON組件時,給的例子無法動態加載ICON。
后來發現,需要在項目開始的時候 main.ts 中 引入 ant-design/icons-vue 后循環注冊組件。
但是,根據在網上查詢的結果都是在app.mount('#app') 后注冊,導致在頁面使用中經常出現 Icon 組件不顯示問題。
import {createApp} from 'vue'
import App from './App.vue'
import * as Icons from "@ant-design/icons-vue";
const icons: any = Icons;
const app = createApp(App)
app.mount('#app')
for (const i in icons) {
// 循環注冊組件
app.component(i, icons[i]);
}
代碼修改為后頁面初始化可以正常顯示圖標
import {createApp} from 'vue'
import App from './App.vue'
// 導入圖標庫
import * as Icons from "@ant-design/icons-vue";
const app = createApp(App)
// 開始使用全局圖標
const icons: any = Icons;
for (const i in icons) {
// 循環注冊組件
app.component(i, icons[i]);
}
app.mount('#app')
