ant-design-vue 2.x 中Icon 组件如何动态渲染???

发布于 2022-09-12 13:49:01 字数 607 浏览 10 评论 0

vue3.0
ant-design-vue 2.x 中
https://2x.antdv.com/componen...
Icon组件都推荐使用<WindowsOutlined /> 导入组件
放弃之前的 <a-icon type=""/>
那如果我想动态渲染 一组Icon。应该怎么写,如果使用antd自带的Icon,不考虑使用其他插件?

<a-menu-item v-for="item in list" :key="item.name">
    <此处如何动态放置 icon>
    <span>{{item.name}}</span>
</a-menu-item>

例如 js
const list = [

  {
    icon: 'UserOutlined',
    name: 'Home'
  },
  {
    icon: 'UserOutlined',
    name: 'Home'
  },
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小霸王臭丫头 2022-09-19 13:49:02

如果是iconfont,设置 createFromIconfontCN 方法参数对象中的 scriptUrl,如果是antv可以使用<template is="xxx" />,当然 icon相关组件需要先引入

佼人 2022-09-19 13:49:02

引入所有字体组件

main.js

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import Antd from 'ant-design-vue'
import * as antIcons from '@ant-design/icons-vue'
import 'ant-design-vue/dist/antd.css'

const app = createApp(App)
// 注册组件
Object.keys(antIcons).forEach(key => {
  app.component(key, antIcons[key])
})
// 添加到全局
app.config.globalProperties.$antIcons = antIcons

app.use(Antd).use(router).use(store).mount('#app')

使用:

<template>
    <a-menu-item v-for="item in list" :key="item.name">
        <component :is="$antIcons[item.icon]" />
        <span>{{item.name}}</span>
    </a-menu-item>
</template>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文