Naive UI框架安装和引入使用

Naive UI介绍:

一个 Vue 3 组件库比较完整,主题可调,使用 TypeScript,注意,naive-ui 仅支持 Vue3。

1、 安装
naiva ui
复制代码
// 使用 npm 安装。

npm i -D naive-ui
字体安
复制代码
npm i -D vfonts
2、使用
直接引入(推荐):你可以直接导入组件并使用它。这种情况下,只有导入的组件才会被打包。
复制代码
<template>
  <n-button>naive-ui</n-button>
</template>

<script>
  import { defineComponent } from 'vue'
  import { NButton } from 'naive-ui'

  export default defineComponent({
    components: {
      NButton
    }
  })
</script>

如果你可以使用 setup script,你可以用下面的方式使用组件。

复制代码
<template>
  <n-button>naive-ui</n-button>
</template>

<script setup>
  import { NButton } from 'naive-ui'
</script>
全局安装(不推荐):安装全部组件

失去 tree-shaking 的能力,打包有冗余代码。如果你想全局安装但是不想安装全部组件,请参考按需引入。

复制代码
import { createApp } from 'vue'
import naive from 'naive-ui'

const app = createApp(App)
app.use(naive)

使用

复制代码
<template>
  <n-button>naive-ui</n-button>
</template>
按需引入

Naive UI 支持 tree shaking,组件、语言、主题均可 tree-shaking。默认情况组件主题为亮色,语言为英文,无需额外导入。了解更多关于主题设定的信息,参见调整主题。

手动引入
复制代码
<script>
  import { defineComponent } from 'vue'
  import { NConfigProvider, NInput, NDatePicker, NSpace } from 'naive-ui'
  // theme
  import { createTheme, inputDark, datePickerDark } from 'naive-ui'
  // locale & dateLocale
  import { zhCN, dateZhCN } from 'naive-ui'

  export default defineComponent({
    components: {
      NConfigProvider,
      NInput,
      NDatePicker,
      NSpace
    },
    setup() {
      return {
        darkTheme: createTheme([inputDark, datePickerDark]),
        zhCN,
        dateZhCN
      }
    }
  })
</script>

<template>
  <n-config-provider :theme="darkTheme" :locale="zhCN" :date-locale="dateZhCN">
    <n-space vertical>
      <n-input />
      <n-date-picker />
    </n-space>
  </n-config-provider>
</template>

<style>
  body {
    background: black;
  }
</style>
自动引入
  1. 可以使用 unplugin-auto-import 插件来自动导入 API。

如果使用模板方式进行开发,可以使用 unplugin-vue-components 插件来按需自动加载组件,插件会自动解析模板中的使用到的组件,并导入组件。

复制代码
// vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      imports: [
        'vue',
        {
          'naive-ui': [
            'useDialog',
            'useMessage',
            'useNotification',
            'useLoadingBar'
          ]
        }
      ]
    }),
    Components({
      resolvers: [NaiveUiResolver()]
    })
  ]
})
按需全局安装组件(手动)
复制代码
import { createApp } from 'vue'
import {
  // create naive ui
  create,
  // component
  NButton
} from 'naive-ui'

const naive = create({
  components: [NButton]
})

const app = createApp()
app.use(naive)
3、配置字体

Naive UI 可以和 vfonts 配合,你可以简单的引入 vfonts 中的字体,包含常规字体和等宽字体。

只需要在你 App 的入口文件导入字体,即可调整 Naive UI 的字体。

复制代码
// 你 App 的入口 js 文件
// ...

// 通用字体
import 'vfonts/Lato.css'
// 等宽字体
import 'vfonts/FiraCode.css'

const app = createApp()
app.use(naive)

// ...

注意:不同 vfonts 字体提供的字重不同,在使用 Lato、OpenSans 的时候你需要全局调整 naive-ui 的字重配置。

复制代码
<!-- 调整 naive-ui 的字重配置 -->
<n-config-provider :theme-overrides="{ common: { fontWeightStrong: '600' } }">
  <app />
</n-config-provider>

注意:具体使用请参考官网:Naive UI

相关推荐
传奇开心果编程11 小时前
【ArkUI进阶练中学】第11课:安全与合规进阶
学习·ui·华为·harmonyos
传奇开心果编程16 小时前
【ArkUI进阶练中学】第12课:应用架构演进与遗留系统迁移
学习·ui·华为·harmonyos
海盗123417 小时前
微软技术日报 2026-10-04:26H2 三个已知问题确认,Blazor 补上智能体 UI
人工智能·microsoft·ui·机器人·aigc
java1234_小锋18 小时前
shadcn/ui 开源项目,专业打造专业UI
ui·开源
传奇开心果编程18 小时前
【ArkUI进阶练中学】第13课:元服务与卡片开发
学习·ui·华为·harmonyos
传奇开心果编程19 小时前
【ArkUI进阶练中学】第19课:AI安全与隐私治理
学习·ui·华为·harmonyos
传奇开心果编程20 小时前
【ArkUI进阶练中学】第18课:Agent亲和架构与应用智能化改造
学习·ui·华为·harmonyos
传奇开心果编程2 天前
【ArkUI进阶练中学】第9课:分布式能力与多设备协同
学习·ui·华为·harmonyos
笨笨饿2 天前
#141_ Codex 本地 AI 代理工具链工作流重构:Headroom、Scrapling、Archify 统一接入
开发语言·人工智能·stm32·单片机·嵌入式硬件·ui·重构
特立独行的猫a3 天前
用仓颉写一个 Tauri:IPC 的每次往返实现原理(web层到仓颉层的触发过程)
前端·ui·harmonyos·tauri·鸿蒙·仓颉