1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Vue3+TS + element-plus 项目 动态生成Icon图标

Vue3+TS + element-plus 项目 动态生成Icon图标

时间:2019-10-22 23:18:35

相关推荐

Vue3+TS + element-plus 项目 动态生成Icon图标

1,使用

文档原话:如果你想像用例一样直接使用,你需要全局注册组件,才能够直接在项目里使用

在main.ts中先导入

import * as Icons from '@element-plus/icons'

.1,方式一

在main.ts中

import {createApp } from 'vue'import App from './App.vue'import router from './router'import {store, key } from './store'import * as Icons from '@element-plus/icons'const app = createApp(App)app.use(store, key)app.use(router)app.mount('#app')// 注册全局组件**Object.keys(Icons).forEach(key => {ponent(key, Icons[key as keyof typeof Icons])})

在xxx.vue文件中

// html<template><el-icon :size="20"><alarm-clock /></el-icon></template>

//或使用动态组件

// html<template><component class="xxx" :is="iconName"></component></template>// scriptexport default {name: 'Login',setup() {const iconName = 'Search'return {iconName}}}

2.2,方式二

在main.ts中

import {createApp, createVNode } from 'vue'import App from './App.vue'import router from './router'import {store, key } from './store'import * as Icons from '@element-plus/icons'const app = createApp(App)app.use(store, key)app.use(router)app.mount('#app')// 创建Icon组件const Icon = (props: {icon: string }) => {const {icon } = propsreturn createVNode(Icons[icon as keyof typeof Icons])}// 注册Icon组件ponent('Icon', Icon)

使用动态组件

// html<template><Icon class="xxx" :icon="iconName"></Icon></template>// scriptexport default {name: 'Login',setup() {const iconName = 'Search'return {iconName}}}

原文链接:/pdd11997110103/article/details/121440220

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。