【Nuxt】为 lucide-vue-next 配置自动导入

  1. 1. 安装依赖
  2. 2. 配置 nuxt.config.ts
  3. 3. 使用
  4. 4. Nuxt 特有坑
  5. 5. 💡 更“Nuxt 风格”的替代方案

安装依赖

1
2
npm i -D unplugin-vue-components
npm i lucide-vue-next

配置 nuxt.config.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// nuxt.config.ts
import Components from 'unplugin-vue-components/vite'

export default defineNuxtConfig({
vite: {
plugins: [
Components({
resolvers: [
(name) => {
if (name.startsWith('Icon')) {
return {
name: name.slice(4),
from: 'lucide-vue-next'
}
}
}
]
})
]
}
})

使用

1
2
3
4
5
6
<template>
<div>
<IconAlertCircle />
<IconActivity />
</div>
</template>

无需 import,Nuxt 自动处理。

Nuxt 特有坑

❌ 1. SSR hydration mismatch

如果你写:

1
<IconHome v-if="process.client" />

会导致 SSR 和客户端渲染不一致,报 hydration mismatch 错误。
✅ 解决方法:使用 ClientOnly。

1
2
3
<ClientOnly>
<IconHome />
</ClientOnly>

❌ 2. 类型提示缺失(TS)

1
2
3
Components({
dts: true
})

会生成 components.d.ts,但默认不包含自动导入的组件。

❌ 3. 图标名大小写

1
2
<Iconhome /> ❌
<IconHome /> ✅

💡 更“Nuxt 风格”的替代方案

如果你想更 Nuxt 原生一点,可以封装一层:

1
2
3
4
5
6
7
8
<!-- components/Icon.vue -->
<script setup lang="ts">
defineProps<{ name: string }>()
</script>

<template>
<component :is="name" />
</template>

然后:

1
2
3
4
<template>
<Icon name="IconHome" />
<Icon name="IconAlertCircle" />
</template>

👉 但这个方式:

  • ❌ 会失去 tree-shaking(不推荐)
投喂小莫
给快要饿死的小莫投喂点零食吧~
投喂小莫
分享
分享提示信息