0


Vue3+Vite+Pinia+Naive后台管理系统搭建之四:Naive UI 组件库的安装和使用

前言

如果对 vue3 的语法不熟悉的,可以移步 Vue3.0 基础入门Vue3.0 基础入门快速入门。

UI 组件请参考官网:Naive Ui 官网

为什么选择 naive ui 不继续用 element ui,因为尤大大推荐,可以尝试下,而且 naive ui 更贴近 vue3 的语法,当然易上手还是element ui 好一点。

github 开源库:Vue3-Vite-Pinia-Naive-Js

gitee 开源库:Vue3-Vite-Pinia-Naive-Js

1. 安装依赖

  1. yarn add naive-ui -D
  2. // or
  3. npm install naive-ui -D

2. 在 SFC (单文件组件) 中使用

直接引入(推荐),你可以直接导入组件并使用它。这种情况下,只有导入的组件才会被打包。

2.1 编辑 src/pages/login.vue 引入naive-ui 组件
  1. <script setup>
  2. import router from "@/router/index.js";
  3. import { NButton } from "naive-ui";
  4. let handleLogin = () => {
  5. router.push({ name: 'home' })
  6. }
  7. </script>
  8. <template>
  9. <div class="login">
  10. <n-button type="primary" size="small" @click="handleLogin">登录</n-button>
  11. </div>
  12. </template>
  13. <style lang="scss" scoped></style>

3. 编辑 src/App.vue 引入 naive-ui 组件

  1. <script setup>
  2. import MessageApi from "@/components/MessageApi.vue";
  3. import {
  4. NMessageProvider,
  5. NDialogProvider,
  6. NConfigProvider,
  7. zhCN,
  8. dateZhCN,
  9. } from "naive-ui";
  10. </script>
  11. <template>
  12. <!-- 如果你想使用信息,你需要把调用其方法的组件放在 n-message-provider 内部并且使用 useMessage 去获取 API。 -->
  13. <n-message-provider>
  14. <!-- 将 message API 通过 message-api 组件注入 window.$msg,之后在其他 SFC 可以直接使用 window.$msg -->
  15. <message-api></message-api>
  16. </n-message-provider>
  17. <!-- 如果你想使用对话框,你需要把调用其方法的组件放在 n-dialog-provider 内部并且使用 useDialog 去获取 API。 -->
  18. <n-dialog-provider>
  19. <!-- 将 n-config-provider 的 locale 设为从 naive-ui 导入的 zhCN 来设定全局中文。 -->
  20. <!-- 将 n-config-provider 的 date-locale 设为从 naive-ui 导入的 dateZhCN 来设定全局日期中文。 -->
  21. <n-config-provider :locale="zhCN" :date-locale="dateZhCN">
  22. <router-view></router-view>
  23. </n-config-provider>
  24. </n-dialog-provider>
  25. </template>
  26. <style scoped></style>

4. 新增 src/components/MessageApi.vue 全局注册 window.$msg 组件

  1. <script setup>
  2. import { useMessage } from 'naive-ui'
  3. window.$msg = useMessage();
  4. </script>
  5. <template>
  6. <div></div>
  7. </template>

5. 编辑 src/pages/home.vue 引入 naive-ui 组件

  1. <script setup>
  2. import router from "@/router/index.js";
  3. import { NButton, useDialog } from "naive-ui";
  4. let toPage = (name) => {
  5. router.push({ name });
  6. };
  7. let handleShowMsg = () => {
  8. window.$msg.success("success message");
  9. };
  10. const dialog = useDialog();
  11. let handleShowDialog = () => {
  12. dialog.warning({
  13. title: "警告",
  14. content: "你确定?",
  15. positiveText: "确定",
  16. negativeText: "不确定",
  17. onPositiveClick: () => {
  18. window.$msg.success("确定");
  19. },
  20. onNegativeClick: () => {
  21. window.$msg.error("不确定");
  22. },
  23. });
  24. };
  25. </script>
  26. <template>
  27. <div class="home">
  28. home
  29. <n-button @click="toPage('demo')" type="primary">goDemo</n-button>
  30. <n-button @click="toPage('login')" type="warning">goLogin</n-button>
  31. <n-button @click="handleShowMsg" type="info">show message</n-button>
  32. <n-button @click="handleShowDialog" type="error">show dialog</n-button>
  33. </div>
  34. </template>
  35. <style lang="scss" scoped></style>

综上

Naive UI 安装完成。下一章: Vue3+Vite+Pinia+Naive后台管理系统搭建之五:Pinia 状态管理


本文转载自: https://blog.csdn.net/weixin_64684095/article/details/131644674
版权归原作者 yqcoder 所有, 如有侵权,请联系我们删除。

“Vue3+Vite+Pinia+Naive后台管理系统搭建之四:Naive UI 组件库的安装和使用”的评论:

还没有评论