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 字体提供的字重不同,在使用 LatoOpenSans 的时候你需要全局调整 naive-ui 的字重配置。

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

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

相关推荐
cwtlw9 小时前
PhotoShop学习01
笔记·学习·ui·photoshop
liuyang___2 天前
element ui的select选择框
前端·javascript·vue.js·ui·elementui
UI设计兰亭妙微2 天前
ui设计公司兰亭妙微分享:科研单位UI界面设计
ui·信息可视化
我家媳妇儿萌哒哒2 天前
vue+element ui 实现选择季度组件
vue.js·ui·elementui
恋猫de小郭3 天前
Flutter 上的 Platform 和 UI 线程合并是怎么回事?它会带来什么?
flutter·ui
萧鼎3 天前
智能自动化新纪元:AI与UiPath RPA的协同应用场景与技术实践
人工智能·ui·rpa
2401_899104114 天前
金属色渐变在UI设计中怎么用?
ui
小盼江5 天前
水果生鲜农产品推荐系统 协同过滤余弦函数推荐水果生鲜农产品 Springboot Vue Element-UI前后端分离 代码+开发文档+视频教程
vue.js·spring boot·ui
-优势在我5 天前
Android TabLayout 实现随意控制item之间的间距
android·java·ui
虚假程序设计5 天前
python用 PythonNet 从 Python 调用 WPF 类库 UI 用XAML
python·ui·wpf