-
-
Notifications
You must be signed in to change notification settings - Fork 144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic icon components #5
Comments
No, this plugin is designed to be used statically. Can you combine it with vite-plugin-purge-icons to have both SSR icons and runtime dynamic icons. |
Oh okay, gotcha. I'll give that a shot, thanks! |
Have you make it work with vite-plugin-purge-icons? |
Due to the mechanism of how unplugin-vue-components works, there is no way to infer a component from a dynamic string. ( The workarounds for this is: <IconA v-if="cond1"/>
<IconB v-else-if="cond2"/>
<IconC v-else-if="cond3"/>
<IconD v-else /> Or <script setup>
import MdiAlarm from '~icons/mdi/alarm'
import MdiAccount from '~icons/mdi/account'
const items = [
{
name: 'Alarm',
icon: MdiAlarm,
},
{
name: 'Account',
icon: MdiAccount,
}
]
</script>
<template>
<div v-for="i of items">
<component :is="i.icon"/>
</div>
</template> |
Along with autoImport one can achieve even more elegant solution. Use the same IconResolver you used in unplugin components:
Then you don't need to even import and the above will look like this:
|
你这么写没用吧 |
Alternatively this component method can also be used to get the icons dynamically rendered using string. Is it advisable to use it? @antfu |
@husayt it works! |
Uncaught (in promise) ReferenceError: IconMdiAlarm is not defined |
Just found that |
Doing this on multiple sites at the moment without issues. May want to make sure you have explicitly enabled the collection via |
unocss Icons preset is welcome. |
我项目中涉及到大量icon,如果每一个icon都要一个vue文件,再导入那就太恶心了 |
谢谢,按照你的方法至少我不用再创建一个个vue文件了 |
Is it possible to dynamically use icons?
I'm using the vitesse template and would previously use something like
<Icon :icon="myIconVariable" />
but now it seems like that no longer exists so I was wondering if it is still possible to do something like that.I tried something like this without much luck
The text was updated successfully, but these errors were encountered: