Skip to content

Lucide Vue

Vue 2 组件,用于 Lucide 图标,能够与 Vue 的选项 API 和模板系统集成。每个图标都是一个 Vue 组件,以内联 SVG 的形式渲染,为仍在使用 Vue 2 的旧版应用提供了熟悉的 Vue 开发模式。

🌐 Vue 2 components for Lucide icons that integrate with Vue's Options API and template system. Each icon is a Vue component that renders as an inline SVG, providing familiar Vue development patterns for legacy applications still using Vue 2.

你可以完成的事:

  • 将图标用作 Vue 2 组件,并集成 Options API
  • 使用现代图标组件维护旧版 Vue 2 应用
  • 与 Vue 2 的模板系统和组件生命周期集成
  • 使用 Vue 2 熟悉的语法和模式构建应用
  • 在计划迁移到 Vue 3 的同时弥补差距

安装

🌐 Installation

sh
pnpm add @lucide/vue
sh
yarn add @lucide/vue
sh
npm install @lucide/vue
sh
bun add @lucide/vue

如何使用

🌐 How to use

Lucide 基于 ES 模块构建,因此完全支持 tree-shaking。

🌐 Lucide is built with ES Modules, so it's completely tree-shakable.

每个图标都可以作为 Vue 组件导入,该组件渲染一个内联 SVG 元素。这样,只有导入到项目中的图标会包含在最终的打包文件中,其余的图标将被进行树摇优化而移除。

🌐 Each icon can be imported as a Vue component, which renders an inline SVG Element. This way only the icons that are imported into your project are included in the final bundle. The rest of the icons are tree-shaken away.

示例

🌐 Example

你可以传递其他属性来调整图标。

🌐 You can pass additional props to adjust the icon.

vue
<script setup>
import { Camera } from '@lucide/vue';
</script>

<template>
  <Camera
    color="red"
    :size="32"
  />
</template>

属性

🌐 Props

名称类型默认值
size数字24
color字符串currentColor
stroke-width数字2
absoluteStrokeWidth布尔值false
default-class字符串lucide-icon

应用属性

🌐 Applying props

要自定义图标的外观,你可以将自定义属性作为 props 直接传递给组件。该组件接受所有 SVG 属性作为 props,这允许对 SVG 元素进行灵活的样式设置。请参阅 MDN 上的 SVG 展示属性列表。

🌐 To customize the appearance of an icon, you can pass custom properties as props directly to the component. The component accepts all SVG attributes as props, which allows flexible styling of the SVG elements. See the list of SVG Presentation Attributes on MDN.

vue
<template>
  <Camera fill="red" />
</template>

使用 Lucide Lab 或自定义图标

🌐 With Lucide Lab or custom icons

Lucide Lab 是一组不属于 Lucide 主库的图标。

它们可以通过使用 Icon 组件来使用。所有像常规 Lucide 图标一样的属性都可以传递以调整图标的外观。

🌐 They can be used by using the Icon component. All props like regular Lucide icons can be passed to adjust the icon appearance.

使用 Icon 组件

🌐 Using the Icon component

这将根据传递的 iconNode 创建一个图标,并渲染一个 Lucide 图标组件。

🌐 This creates a single icon based on the iconNode passed and renders a Lucide icon component.

vue
<script setup>
import { Icon } from '@lucide/vue';
import { baseball } from '@lucide/lab';
</script>

<template>
  <Icon :iconNode="baseball" />
</template>

一个通用图标组件

🌐 One generic icon component

可以创建一个通用的图标组件来加载图标,但不建议这样做。

🌐 It is possible to create one generic icon component to load icons, but it is not recommended.

DANGER

下面的示例导入了所有的 ES 模块,所以在使用时请谨慎。导入所有图标会显著增加应用的构建大小,从而对性能产生负面影响。在使用像 WebpackRollupVite 这样的打包工具时,这一点尤为重要。

图标组件示例

🌐 Icon Component Example

vue
<script setup>
import { computed } from 'vue';
import * as icons from "@lucide/vue";

const props = defineProps({
  name: {
    type: String,
    required: true
  },
  size: Number,
  color: String,
  strokeWidth: Number,
  defaultClass: String
})

const icon = computed(() => icons[props.name]);
</script>

<template>
  <component
    :is="icon"
    :size="size"
    :color="color"
    :stroke-width="strokeWidth" :default-class="defaultClass"
  />
</template>

使用图标组件

🌐 Using the Icon Component

上面列出的所有其他属性也适用于 Icon 组件。

🌐 All other props listed above also work on the Icon Component.

vue
<template>
  <div id="app">
    <Icon name="Airplay" />
  </div>
</template>

无障碍

🌐 Accessibility

默认情况下,我们使用 aria-hidden="true" 从屏幕阅读器中隐藏图标。

🌐 By default, we hide icons from screen readers using aria-hidden="true".

你可以使用 aria-label 添加可访问性属性。

🌐 You can add accessibility attributes using aria-labels.

vue
<script setup>
import { Check } from 'lucide-vue-next';
</script>

<template>
  <Check
    color="red"
    :size="32"
    aria-label="Task completed"
  />
</template>

有关无障碍最佳实践,请参阅我们的无障碍指南

🌐 For best practices on accessibility, please see our accessibility guide.