当前位置:   article > 正文

Naive UI——一个 Vue 3 组件库(Naive UI的安装和使用)_navie ui

navie ui

一.NaiveUI 一个VUE3组件库

        Naive UI
        注意:naive-ui 仅支持 Vue3。如果你在使用 Vue2,可以去看看别的库。

二.安装 

1.npm

        使用npm 安装。

npm i -D naive-ui

2.字体

npm i -D vfonts

        (1) 配置字体

        Naive UI 可以和 vfonts 配合,你可以简单的引入 vfonts 中的字体,包含常规字体和等宽字体。只需要在你 App 的入口文件导入字体,即可调整 Naive UI 的字体。

  1. // 你 App 的入口 js 文件
  2. // ...
  3. // 通用字体
  4. import 'vfonts/Lato.css'
  5. // 等宽字体
  6. import 'vfonts/FiraCode.css'
  7. const app = createApp()
  8. app.use(naive)
  9. // ...

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

  1. <!-- 调整 naive-ui 的字重配置 -->
  2. <n-config-provider :theme-overrides="{ common: { fontWeightStrong: '600' } }">
  3. <app />
  4. </n-config-provider>

3.图标

        naive-ui 建议使用 xicons 作为图标库。

  1. <template>
  2. <n-icon size="40">
  3. <game-controller-outline />
  4. </n-icon>
  5. <n-icon size="40" color="#0e7a0d">
  6. <game-controller />
  7. </n-icon>
  8. <n-icon size="40" :component="GameController" />
  9. </template>
  10. <script lang="ts">
  11. import { GameControllerOutline, GameController } from '@vicons/ionicons5'
  12. import { defineComponent } from 'vue'
  13. export default defineComponent({
  14. components: {
  15. GameController,
  16. GameControllerOutline
  17. },
  18. setup () {
  19. return {
  20. GameController
  21. }
  22. }
  23. })
  24. </script>

三.按需引入

Naive UI 支持 tree shaking,组件、语言、主题均可 tree-shaking。

默认情况组件主题为亮色,语言为英文,无需额外导入。

1.手动引入

  1. <script>
  2. import { defineComponent } from 'vue'
  3. import { NConfigProvider, NInput, NDatePicker, NSpace } from 'naive-ui'
  4. // theme
  5. import { createTheme, inputDark, datePickerDark } from 'naive-ui'
  6. // locale & dateLocale
  7. import { zhCN, dateZhCN } from 'naive-ui'
  8. export default defineComponent({
  9. components: {
  10. NConfigProvider,
  11. NInput,
  12. NDatePicker,
  13. NSpace
  14. },
  15. setup() {
  16. return {
  17. darkTheme: createTheme([inputDark, datePickerDark]),
  18. zhCN,
  19. dateZhCN
  20. }
  21. }
  22. })
  23. </script>
  24. <template>
  25. <n-config-provider :theme="darkTheme" :locale="zhCN" :date-locale="dateZhCN">
  26. <n-space vertical>
  27. <n-input />
  28. <n-date-picker />
  29. </n-space>
  30. </n-config-provider>
  31. </template>
  32. <style>
  33. body {
  34. background: black;
  35. }
  36. </style>

2.自动引入

可以使用 unplugin-auto-import 插件来自动导入 API。

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

  1. // vite.config.ts
  2. import { defineConfig } from 'vite'
  3. import vue from '@vitejs/plugin-vue'
  4. import AutoImport from 'unplugin-auto-import/vite'
  5. import Components from 'unplugin-vue-components/vite'
  6. import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
  7. // https://vitejs.dev/config/
  8. export default defineConfig({
  9. plugins: [
  10. vue(),
  11. AutoImport({
  12. imports: [
  13. 'vue',
  14. {
  15. 'naive-ui': [
  16. 'useDialog',
  17. 'useMessage',
  18. 'useNotification',
  19. 'useLoadingBar'
  20. ]
  21. }
  22. ]
  23. }),
  24. Components({
  25. resolvers: [NaiveUiResolver()]
  26. })
  27. ]
  28. })

3.按需全局安装组件(手动)

  1. import { createApp } from 'vue'
  2. import {
  3. // create naive ui
  4. create,
  5. // component
  6. NButton
  7. } from 'naive-ui'
  8. const naive = create({
  9. components: [NButton]
  10. })
  11. const app = createApp()
  12. app.use(naive)

安装后,你可以这样在 SFC 中使用你安装的组件。

  1. <template>
  2. <n-button>naive-ui</n-button>
  3. </template>

四.调整主题

Naive UI 通过使用 n-config-provider 调整主题。

默认情况下所有组件均为亮色主题,无需任何配置。

 1.使用暗色主题

将 n-config-provider 的 theme 设为从 naive-ui 导入的 darkTheme 来设定暗色主题。

若 theme 为 undefined 则不会影响内部组件的主题。

  1. <template>
  2. <n-config-provider :theme="darkTheme">
  3. <app />
  4. </n-config-provider>
  5. </template>
  6. <script>
  7. import { defineComponent } from 'vue'
  8. import { darkTheme } from 'naive-ui'
  9. export default defineComponent({
  10. setup() {
  11. return {
  12. darkTheme
  13. }
  14. }
  15. })
  16. </script>

2.调整主题变量

你不需要写任何 CSS(Scss、Less...)。

配置的全局主题变量会对后代组件生效的主题变量覆盖。

通过设定 n-config-provider 的 theme-overrides 来调整主题变量。naive-ui 导出了 GlobalThemeOverrides 类型帮助你定义主题。

具体可使用变量请参考 GlobalThemeOverrides 类型提示。

如果想要查看更多的主题变量,可在 Naive UI 主页的右下角的 edit 按钮查看。

可以修改对应的主题变量,导出后可以拿到 themeOverrides 对象。

  1. <script>
  2. import { NConfigProvider } from 'naive-ui'
  3. /**
  4. * js 文件下使用这个做类型提示
  5. * @type import('naive-ui').GlobalThemeOverrides
  6. */
  7. const themeOverrides = {
  8. common: {
  9. primaryColor: '#FF0000'
  10. },
  11. Button: {
  12. textColor: '#FF0000'
  13. },
  14. Select: {
  15. peers: {
  16. InternalSelection: {
  17. textColor: '#FF0000'
  18. }
  19. }
  20. }
  21. // ...
  22. }
  23. // ...
  24. </script>
  25. <template>
  26. <n-config-provider :theme-overrides="themeOverrides">
  27. <my-app />
  28. </n-config-provider>
  29. </template>

  1. <template>
  2. <n-input
  3. v-model:value="formInline.password"
  4. type="password"
  5. @keyup.enter="handleSubmit"
  6. maxlength="16"
  7. show-password-on="click"
  8. :placeholder="$t('global.form_password')"
  9. :theme-overrides="InputThemeOverrides">
  10. </n-input>
  11. <n-checkbox v-model:checked="autoLogin" :theme-overrides="CheckboxThemeOverrides">复选框</n-checkbox>
  12. <n-button
  13. type="primary"
  14. @click="handleSubmit"
  15. size="large"
  16. :loading="loading"
  17. block
  18. class="button-text"
  19. :theme-overrides="buttonThemeOverrides">按钮</n-button>
  20. </template>
  21. <script lang='ts' setup>
  22. import { InputProps, CheckboxProps, ButtonProps } from 'naive-ui'
  23. type InputThemeOverrides = NonNullable<InputProps['themeOverrides']>
  24. type CheckboxThemeOverrides = NonNullable<CheckboxProps['themeOverrides']>
  25. type ButtonThemeOverrides = NonNullable<ButtonProps['themeOverrides']>
  26. const InputThemeOverrides: InputThemeOverrides = {
  27. textColor: 'rgb(51, 54, 57)',
  28. color: 'rgba(255, 255, 255, 1)',
  29. colorFocus: 'rgba(255, 255, 255, 1)',
  30. iconColor: 'rgba(194, 194, 194, 1)'
  31. }
  32. const CheckboxThemeOverrides: CheckboxThemeOverrides = {
  33. checkMarkColor: '#FFF'
  34. }
  35. const buttonThemeOverrides: ButtonThemeOverrides = {
  36. textColorPrimary: 'rgba(255, 255, 255, 1)',
  37. textColorHoverPrimary: 'rgba(255, 255, 255, 1)'
  38. }
  39. </script>

3.TS 下使用主题变量

如果你正在使用 ts 写代码,这块比较适合你。

  1. <script lang="ts">
  2. import { NConfigProvider, GlobalThemeOverrides } from 'naive-ui'
  3. const themeOverrides: GlobalThemeOverrides = {
  4. common: {
  5. primaryColor: '#FF0000'
  6. },
  7. Button: {
  8. textColor: '#FF0000'
  9. }
  10. }
  11. // ...
  12. </script>
  13. <template>
  14. <n-config-provider :theme-overrides="themeOverrides">
  15. <my-app />
  16. </n-config-provider>
  17. </template>

4.调整组件主题变量

组件主题变量使用方法同全局主题变量使用方法,并且组件主题变量会覆盖全局主题变量。

  1. <script lang="ts">
  2. import { SelectProps, ButtonProps } from 'naive-ui'
  3. type SelectThemeOverrides = NonNullable<SelectProps['themeOverrides']>
  4. type ButtonThemeOverrides = NonNullable<ButtonProps['themeOverrides']>
  5. const selectThemeOverrides: SelectThemeOverrides = {
  6. menuBoxShadow:
  7. '0 6px 16px -9px rgba(0, 0, 0, .08), 0 9px 28px 0 rgba(0, 0, 0, .05), 0 12px 48px 16px rgba(0, 0, 0, .03)',
  8. peers: {
  9. InternalSelection: {
  10. textColor: '#FF0000',
  11. heightMedium: '42px'
  12. }
  13. }
  14. }
  15. const buttonThemeOverrides: ButtonThemeOverrides = {
  16. heightMedium: '40px',
  17. textColor: 'rgba(24, 127, 231, 1)'
  18. }
  19. // ...
  20. </script>
  21. <template>
  22. <n-select
  23. v-model:value="value"
  24. :options="options"
  25. :theme-overrides="selectThemeOverrides"
  26. />
  27. <n-button :theme-overrides="buttonThemeOverrides">theme</n-button>
  28. </template>

5.不同主题下调整主题变量

如果你想要在亮色和暗色上同时使用不同的主题变量,可以来看看这个。

  1. <script>
  2. import { NConfigProvider, darkTheme } from 'naive-ui'
  3. /**
  4. * @type import('naive-ui').GlobalThemeOverrides
  5. */
  6. const lightThemeOverrides = {
  7. common: {
  8. primaryColor: '#000000'
  9. }
  10. // ...
  11. }
  12. const darkThemeOverrides = {
  13. common: {
  14. primaryColor: '#FFFFFF'
  15. }
  16. // ...
  17. }
  18. const theme = null
  19. // ...
  20. </script>
  21. <template>
  22. <n-config-provider
  23. :theme="theme"
  24. :theme-overrides="theme === null ? lightThemeOverrides : darkThemeOverrides"
  25. >
  26. <my-app />
  27. </n-config-provider>
  28. </template>

6.使用 peers 主题变量

很多时候组件内部都会复用另一个组件,因此出现了 peers 的主题变量。

peers 相关的主题变量还没有暴露,使用 GlobalThemeOverrides 可以查看对应组件的 peers 变量。

具体哪些可使用的 peers 后续会更新。

  1. <script lang="ts">
  2. import { NConfigProvider, GlobalThemeOverrides } from 'naive-ui'
  3. const themeOverrides: GlobalThemeOverrides = {
  4. Select: {
  5. peers: {
  6. InternalSelection: {
  7. textColor: '#FF0000'
  8. },
  9. InternalSelectMenu: {
  10. borderRadius: '6px'
  11. }
  12. }
  13. },
  14. DataTable: {
  15. paginationMargin: '40px 0 0 0',
  16. peers: {
  17. Empty: {
  18. textColor: '#ccc'
  19. },
  20. Pagination: {
  21. itemTextColor: '#ccc'
  22. }
  23. }
  24. }
  25. // ...
  26. }
  27. // ...
  28. </script>
  29. <template>
  30. <n-config-provider :theme-overrides="themeOverrides">
  31. <my-app />
  32. </n-config-provider>
  33. </template>

7.同步 body 元素的样式

出于以下原因,你可能需要将某些样式设定在 document.body 上。

  1. naive-ui 会设定一些非响应式的全局样式(例如字体),它们在默认状况下工作良好,但是不能响应主题的变化。
  2. n-config-provider 无法将全局样式同步到它以外的地方(例如 body 背景色)。

通过使用 n-global-style 可以将常见的全局样式同步到 body 上。在下面的例子中,n-global-style 会将 n-config-provider 提供的主题同步到 document.body 上。

  1. <template>
  2. <n-config-provider>
  3. <app />
  4. <n-global-style />
  5. </n-config-provider>
  6. </template>

8.主题编辑器

naive-ui 提供主题编辑器帮助你方便的编辑主题并导出对应配置。它可以被嵌套于 n-config-provider 中。

主题编辑器不包含在全局安装中(app.use(naive))。你需要显式引入来使用它。

  1. <template>
  2. <n-theme-editor>
  3. <app />
  4. </n-theme-editor>
  5. </template>
  6. <script>
  7. import { defineComponent } from 'vue'
  8. import { NThemeEditor } from 'naive-ui'
  9. export default defineComponent({
  10. components: {
  11. NThemeEditor
  12. }
  13. })
  14. </script>

了解更多关于 n-config-provider 的信息,参见 全局化配置。 

五.全局化配置 Config Provider

全局化配置设置内部组件的主题、语言和组件卸载于其他位置的 DOM 的类名。

1.设置 n-config-provider 内部组件的主题。

  1. <template>
  2. <n-config-provider :theme="theme">
  3. <n-card>
  4. <n-space>
  5. <n-button @click="theme = darkTheme">
  6. 深色
  7. </n-button>
  8. <n-button @click="theme = null">
  9. 浅色
  10. </n-button>
  11. </n-space>
  12. </n-card>
  13. </n-config-provider>
  14. </template>
  15. <script lang="ts">
  16. import { defineComponent, ref } from 'vue'
  17. import { darkTheme } from 'naive-ui'
  18. import type { GlobalTheme } from 'naive-ui'
  19. export default defineComponent({
  20. setup () {
  21. return {
  22. darkTheme,
  23. theme: ref<GlobalTheme | null>(null)
  24. }
  25. }
  26. })
  27. </script>

2.API

名称类型默认值说明版本
abstractbooleanfalse是否不存在 DOM 包裹
breakpoints{ [k: string]: number }{ xs: 0, s: 640, m: 1024, l: 1280, xl: 1536, xxl: 1920 }屏幕响应式断点,对 n-grid 生效。这个属性不是响应式的,你需要在组件第一次挂载时就设定好
cls-prefixstringn内部所有组件的类的前缀,仅首次设定会生效
date-localeDateLocale | nullundefined对后代组件生效的日期语言对象,为 null 时会使用默认 dateEnUS,为 undefined 时会继承上级 n-config-provider
inline-theme-disabledbooleanfalse是否禁用 inline css 主题变量,如果你不会频繁调整主题变量,并且需要 SSR 或者想让 devtools 看起来更干净,可以打开这个选项。注意,这个属性不是响应式的2.26.0
katexobjectundefined公式组件需要的 katex 对象2.34.0
localeLocale | nullundefined对后代组件生效的语言对象,为 null 时会使用默认 enUS,为 undefined 时会继承上级 n-config-provider
namespacestringundefinedn-config-provider 内部组件被卸载于其他位置的 DOM 的类名
preflight-style-disabledbooleanfalse是否禁用默认样式,如果你禁用了它,便可以完全控制全局样式。你也可以使用 n-global-style 去挂载全局样式(推荐,样式是响应式的)2.29.0
tagstring'div'n-config-provider 被渲染成的元素
themeTheme | nullundefined对后代组件生效的主题对象,为 null 时会使用默认亮色,为 undefined 时会继承上级 n-config-provider。更多信息参见调整主题
theme-overridesThemeOverrides | nullundefined对后代组件生效的主题变量覆盖,为 null 时会清除全部覆盖变量,为 undefined 时会继承上级 n-config-provider。更多信息参见调整主题

六.支持的平台

1.浏览器

不支持 IE 浏览器。

EdgeFirefoxChromeSafari 等现代浏览器的最新的 2 个版本确保会被支持。

对于这些浏览器的其他版本中,由于开发资源的限制并没有做过严格的测试。但是我们预期 naive-ui 应该在这些浏览器不算太老的版本上能正常的运行(比如 2 年之内的版本)。如果你发现了任何问题欢迎来提 issue。

2.Vue

只支持 Vue 3(>3.0.5)。

3.TypeScript

需要版本 > 4.1。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/107502
推荐阅读
相关标签
  

闽ICP备14008679号