当前位置:   article > 正文

vue3 + vite 从零搭建 elemelt plus + axios 封装_vue3 + vite + js + elementplus + axios

vue3 + vite + js + elementplus + axios

前言:

Vite当前仅适用于Vue3.x。这也意味着您不能使用尚未与Vue 3兼容的库,并且vite构建工具要求node 版本在 16+ 以上 node -v 查看当前版本 低于16 可使用 nvm 进行版本管理。

一 环境安装 初始化  

1:脚手架安装

pnpm install -g vite@latest

2:项目初始化

  1. pnpm init vite@latest
  2. Ok to proceed? (y) y
  3. // 输入 y 继续
  4. Project name:
  5. 输入一个项目文件夹名称
  6. ? Select a framework: » - Use arrow-keys. Return to submit.
  7. vanilla
  8. > vue
  9. react
  10. preact
  11. lit
  12. svelte
  13. 选择vue 创建完成

3.1:安装 element-plus 组件及图标库按需引入

  1. pnpm i element-plus
  2. pnpm i unplugin-vue-components -D
  3. pnpm i unplugin-element-plus -D
  4. pnpm i @element-plus/icons-vue
  5. pnpm i unplugin-auto-import -D

3.2:vite.config.js 配置

  1. // element plus按需导入
  2. import AutoImport from 'unplugin-auto-import/vite';
  3. import Components from 'unplugin-vue-components/vite';
  4. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  5. import Icons from "unplugin-icons/vite"
  6. import IconsResolver from "unplugin-icons/resolver"
  7. // 文件内加入
  8. plugins: [
  9. vue(),
  10. AutoImport({
  11. resolvers: [
  12. ElementPlusResolver(),
  13. IconsResolver({
  14. prefix: 'Icon'
  15. })
  16. ],
  17. }),
  18. Components({
  19. resolvers: [
  20. ElementPlusResolver(),
  21. IconsResolver({
  22. enabledCollections: ['ep']
  23. })
  24. ],
  25. }),
  26. Icons({
  27. autoInstall: true
  28. })
  29. ],

4.1:安装 vue-i18n

  1. pnpm install vue-i18n
  2. pnpm i --save-dev @intlify/vite-plugin-vue-i18n

4.2 语言配置 

  1. src/common/i18n/config/zh.js
  2. export default{
  3. message: {
  4. username: '用户名',
  5. password: '密码',
  6. login: '登录',
  7. captcha: '验证码',
  8. forgetPassword: '忘记密码?',
  9. loginTip: '当前登录结果随机。验证码随便填',
  10. editpassword: '修改密码',
  11. logout: '退出登录',
  12. errMsg: {
  13. inputRequired: '请输入{cont}',
  14. selectRequired: '请选择{cont}'
  15. }
  16. },
  17. }
  18. src/common/i18n/config/en.js
  19. export default{
  20. message: {
  21. username: 'User Name',
  22. password: 'Password',
  23. login: 'Login',
  24. captcha: 'Captcha',
  25. forgetPassword: 'Forget Password?',
  26. loginTip: 'The login result is random. Just fill in the captcha',
  27. editpassword: 'Edit Password',
  28. logout: 'Logout',
  29. errMsg: {
  30. inputRequired: 'Please Input {cont}',
  31. selectRequired: 'Please Select {cont}'
  32. }
  33. },
  34. }
  35. src/common/i18n/index.js
  36. import { createI18n } from 'vue-i18n'; //引入vue-i18n组件
  37. import zhCn from './config/zh';
  38. import en from './config/en';
  39. const messages = {
  40. zhCn,
  41. en
  42. }
  43. let defaultLang = sessionStorage.getItem('lang') || 'zhCn';
  44. const i18n = createI18n({
  45. fallbackLocale: defaultLang,//预设语言环境
  46. globalInjection:true,
  47. legacy: false, // you must specify 'legacy: false' option
  48. locale: defaultLang,//默认显示的语言
  49. messages,//本地化的语言环境信息。
  50. });
  51. export default i18n
  52. 组件方法
  53. <template>
  54. <div>
  55. <el-dropdown @command="handleClick">
  56. <el-button type="primary">
  57. VUE国际化
  58. </el-button>
  59. <template #dropdown>
  60. <el-dropdown-menu>
  61. <el-dropdown-item command="en">英文</el-dropdown-item>
  62. <el-dropdown-item command="zhCn">中文</el-dropdown-item>
  63. </el-dropdown-menu>
  64. </template>
  65. </el-dropdown>
  66. </div>
  67. </template>
  68. <script>
  69. import { defineComponent } from "vue";
  70. import { useI18n } from 'vue-i18n';
  71. export default defineComponent({
  72. setup() {
  73. const { locale } = useI18n();
  74. const handleClick = (value) => {
  75. locale.value = value;
  76. sessionStorage.setItem('lang',value);
  77. }
  78. return {
  79. handleClick
  80. };
  81. },
  82. });
  83. </script>
  84. <style scoped>
  85. .example-showcase .el-dropdown + .el-dropdown {
  86. margin-left: 15px;
  87. }
  88. .example-showcase .el-dropdown-link {
  89. cursor: pointer;
  90. color: var(--el-color-primary);
  91. display: flex;
  92. align-items: center;
  93. }
  94. </style>

4.3:mian.js

  1. import i18n from './common/i18n/index';
  2. app.use(i18n);

4.4:vite.config.js

  1. import path from 'path';
  2. // plugins 下面新增
  3. vueI18n({
  4. include: path.resolve(__dirname, './path/to/src/locales/**')
  5. }),
  6. alias: {
  7. 'vue-i18n': 'vue-i18n/dist/vue-i18n.runtime.esm-bundler.js',
  8. },

5.1:vuex 安装

  1. pnpm i vuex@next -S
  2. pnpm i vuex-persistedstate

5.2 配置

  1. src/store/modules/user.js
  2. const user = {
  3. namespaced: true,
  4. state: {
  5. userInfo: null
  6. },
  7. mutations: {
  8. logout (state) {
  9. // 清理账号数据
  10. state.userInfo = null
  11. localStorage.clear()
  12. sessionStorage.clear()
  13. },
  14. setUserInfo (state, info) {
  15. state.userInfo = info
  16. }
  17. }
  18. }
  19. export default user
  20. src/store/store.js
  21. import { createStore } from "vuex";
  22. import createPersistedstate from "vuex-persistedstate";
  23. import user from './modules/user'
  24. export default createStore({
  25. modules: {
  26. user ,
  27. },
  28. plugins: [
  29. //默认是存储在localStorage中
  30. createPersistedstate({
  31. // key: 存储数据的键名
  32. key: "longStore",
  33. //paths:存储state中的那些数据
  34. paths: ["lang"],
  35. }),
  36. ],
  37. });

5.3:mian.js

  1. import store from "./store/store";
  2. app.use(store);

6.1:安装vue-router

pnpm install vue-router@4 -S

6.2:配置 (注:因最近较忙未配置动态路由及路由权限配置,后续补上)

  1. src/router/static-routes.js
  2. // 1.路由出口在App.vue
  3. const singleRoutes = [
  4. {
  5. // 设置初始页面
  6. path: '',
  7. redirect: '/home',
  8. meta: {
  9. requireAuth: true
  10. }
  11. },
  12. ]
  13. /**
  14. * 路由出口在main.vue,包括了菜单项和非菜单页面
  15. * 若该页面属于菜单页面,需要添加type: 'menu'属性
  16. */
  17. const staticTree = [
  18. {
  19. path: '/main',
  20. name: 'main-home',
  21. title: '首页',
  22. icon: 'ios-paper',
  23. type: 'menu',
  24. component: () => import('./../views/lang/index.vue'),
  25. }
  26. ]
  27. const staticMenuList = staticTree
  28. const staticMainRoutes = staticTree
  29. // 汇总所有的路由
  30. const staticRoutes = [...staticMainRoutes, ...singleRoutes]
  31. export { staticRoutes, staticMenuList, staticTree }
  32. src/router/router.js
  33. import {
  34. createRouter,
  35. createWebHashHistory,
  36. // createWebHistory,
  37. } from 'vue-router';
  38. import { staticRoutes } from './static-routes.js'
  39. export default createRouter({
  40. history: createWebHashHistory(),
  41. routes: [...staticRoutes]
  42. })

6.3:mian.js

  1. import router from './router/router';
  2. app.use(router);

7.1:安装less

pnpm i less-loader less -D

7.2:配置 vite.config.js

  1. css: {
  2. preprocessorOptions: {
  3. less: {
  4. modifyVars: {
  5. hack: `true; @import (reference) "${path.resolve("src/assets/less/app.less")}";`,
  6. },
  7. javascriptEnabled: true,
  8. },
  9. },
  10. },

8:配置项目别名

  1. defineConfig({
  2. resolve:{
  3. //设置路径别名
  4. alias: {
  5. '@': path.resolve(__dirname, './src'),
  6. // '*': path.resolve('')
  7. },
  8. },
  9. ....
  10. }

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

闽ICP备14008679号