vue 升级3 +vite+antdv4

news2024/9/23 3:29:49

目录

一、安装包相关升级

二、vite.config

三、 入口文件修改

四、App.vue 及相关升级

 五、路由

六、状态管理VUEX


一、安装包相关升级

升级pakage.json相关安装包

vue2插件vue3替换插件vue2使用vue3使用
vue-ls 
vuex-persistedstate或vuex-persist
Vue.ls.get() 

Vue.ls.set()

直接操作store相关
k-form-design
k-designer
vue-contextmenu
可使用ui组件
<vue-context-menu
:contextMenuData="[]"
@deleteLink="deleteLink">
</vue-context-menu
<a-dropdown :trigger="['contextmenu']">
vcolorpicker
可使用ui组件
@jiaminghi/data-view
echarts
vue-json-viewer
vue3-json-viewer
vue-draggable
vue-draggable-next
moment
dayjs

升级nodejs15版本及以上

1.安装vite

npm i vite -g
npm create vite@latest

#相关目录文件,注意:vue-cli生成index.html在public下要改为根目录

.package.json

删除vue-cli相关,修改

"serve": "vue-cli-service serve",
"build": "vue-cli-service build"

 为

{
  "name": "vue-antd-pro",
  "version": "2.1.0",
  "private": true,
  "scripts": {
    "dev": "vite --mode development",
    "build": "vite build",
    "serve": "vite preview",
    "lint": "eslint --ext .js,.vue --ignore-path .gitignore --fix src",
    "test:unit": "vue-cli-service test:unit",
    "build:preview": "vue-cli-service build --mode preview",
    "postinstall": "opencollective-postinstall"
  },
  "dependencies": {
    "@aspnet/signalr": "^1.0.27",
    "@microsoft/signalr": "^7.0.5",
    "@rollup/plugin-inject": "^5.0.3",
    "@vitejs/plugin-vue-jsx": "^3.0.1",
    "@vue/compat": "^3.3.4",
    "@vueuse/core": "^6.7.3",
    "ant-design-vue": "^4.0.0-rc.4",
    "axios": "^1.4.0",
    "babel-plugin-transform-remove-strict-mode": "0.0.2",
    "babel-polyfill": "^6.26.0",
    "echarts": "^5.4.2",
    "eslint": "^8.40.0",
    "eslint-plugin-vue": "^9.13.0",
    "jquery": "^3.5.1",
    "jquery-ui": "1.13.0",
    "k-designer": "^0.0.42",
    "md5": "^2.2.1",
    "mockjs2": "1.0.8",
    "nprogress": "^0.2.0",
    "vite": "^4.3.9",
    "vite-plugin-svg-icons": "^2.0.1",
    "vue": "^3.3.4",
    "vue-clipboard2": "^0.2.1",
    "vue-codemirror-lite": "^1.0.4",
    "vue-cropper": "^0.5.11",
    "vue-draggable-next": "^2.1.1",
    "vue-qr": "^4.0.9",
    "vue-quill-editor": "^3.0.6",
    "vue-router": "^4.2.0",
    "vue3-json-viewer": "^2.2.2",
    "vuex": "^4.1.0",
    "vuex-persistedstate": "^4.1.0",
    "wangeditor": "^4.7.15",
  },
  "devDependencies": {
    "@ant-design/colors": "^3.2.1",
    "@typescript-eslint/eslint-plugin": "^5.59.6",
    "@typescript-eslint/parser": "^5.59.6",
    "@vitejs/plugin-vue": "^1.6.1",
    "@vitejs/plugin-vue-jsx": "^3.0.1",
    "@vitejs/plugin-vue2": "^2.2.0",
    "@vue/compiler-sfc": "^3.3.4",
    "@vue/eslint-config-prettier": "^7.1.0",
    "@vue/eslint-config-standard": "^8.0.1",
    "@vue/test-utils": "^2.3.2",
    "@zougt/some-loader-utils": "^1.4.3",
    "@zougt/vite-plugin-theme-preprocessor": "^1.4.8",
    "autoprefixer": "^10.4.14",
    "babel-plugin-import": "^1.13.0",
    "babel-plugin-transform-remove-console": "^6.9.4",
    "eslint": "^8.40.0",
    "eslint-config-prettier": "^8.8.0",
    "eslint-plugin-html": "^5.0.0",
    "eslint-plugin-prettier": "^4.2.1",
    "eslint-plugin-vue": "^9.13.0",
    "less": "^3.0.4",
    "less-loader": "^5.0.0",
    "postcss": "^8.4.23",
    "postcss-loader": "^7.3.0",
    "prettier": "^2.8.8",
    "sass": "1.32.0",
    "stylus": "^0.54.8",
    "unplugin-vue-components": "^0.24.1",
    "vite": "^4.3.8",
    "vite-plugin-compression": "^0.5.1",
    "vite-plugin-require": "^1.1.10",
    "vite-plugin-theme": "^0.8.6",
    "vue-eslint-parser": "^9.3.0"
  },
  "main": ".eslintrc.js",
  "directories": {
    "test": "tests"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": ""
}

二、vite.config

const path = require("path");
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vitePluginRequire from 'vite-plugin-require';
import inject from '@rollup/plugin-inject';
import Components from 'unplugin-vue-components/vite'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
import viteCompression from "vite-plugin-compression";
import vueJsx from '@vitejs/plugin-vue-jsx'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
export default defineConfig({
  // base: './',  // 开发或生产环境服务的公共基础路径径
  envDir: './env',
  plugins: [
    vue({
      template: {
        compilerOptions: {
          compatConfig: {
            MODE: 3 // vue2还是vue3模式
          }
        }
      }
    }),
    vueJsx(),
    vitePluginRequire(),
    inject({
      $: "jquery",  // 这里会自动载入 node_modules 中的 jquery   jquery全局变量
      jQuery: "jquery",
      "windows.jQuery": "jquery"
    }),
    createSvgIconsPlugin({
      // 要缓存的图标文件夹
      iconDirs: [path.resolve(__dirname, 'src/assets/icons')],
      // 执行 icon name 的格式
      symbolId: 'icon-[name]'
    }),
    Components({
        dts: 'src/types/components.d.ts',
        include: [/\.vue$/, /\.tsx$/],
        resolvers: [ AntDesignVueResolver({
          // 参数配置可参考:https://github.com/antfu/unplugin-vue-components/blob/main/src/core/resolvers/antdv.ts
          // 自动引入 ant-design/icons-vue中的图标,需要安装@ant-design/icons-vue
          // importStyle: 'less',
          importStyle: false,
          resolveIcons: true,
        })]
    }),
    viteCompression() // gzip
  ],
  // publicDir: 'public',
  cacheDir: 'node_modules/.vite',
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
      'vue': 'vue/dist/vue.esm-bundler.js',
      // vue: '@vue/compat',
    },
    extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'], // 忽略文件后缀

  },
  css: {
    preprocessorOptions: {
      less: {
        javascriptEnabled: true
      }
    }
  },
  // 服务的配置
  server: {
    cors: false,
    // 主机名
    host: '0.0.0.0',
    // 端口
    port: 8080,
    // 端口被占用调用下个端口
    strictPort: false,
    // boolean | String 如果是boolean且为true 会自动打开浏览器。 配置是字符串 会被用作 URL 的路径名
    open: false,
    /*
      服务器代理地址,用于代理到后台的服务器。
    */
    // proxy: createProxy(),
    // force: true // 强制使依赖预构建。
    //
    fs: {
      strict: false
    }
  },
  // 正式环境的配置
  build: {
    // 去除console debug
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true
      }
    },
    target: 'modules',
    // target: 'es2015',
    // 指定输出的路径,相当于项目根目录
    // outDir: mode.includes('production.hr') ? 'dist/hr' : 'dist/uc',
    // 静态资源存放路径,相对于outDir
    // assetsDir: 'public/',
    // 小于此配置的会被base64编码,0为完全禁用
    assetsInlineLimit: 4096,
    // 启用/禁用css代码拆分  如果禁用整个项目所有css将会提取为一个css
    cssCodeSplit: true,
    // boolean| inline 构建后是不是生产source map 文件
    sourcemap: false,
    // 启用和禁用压缩报告
    brotliSize: true,
    // chunk大小警告
    // chunkSizeWarningLimit: 500,
    minify: true,  //打包结果取消minify,方便我们看打包后结果对比 'terser'
    chunkSizeWarningLimit: 500,
    rollupOptions: {
      // input: resolve(__dirname, 'index.html'),
      output: {
        chunkFileNames: 'static/js/[name]-[hash].js',
        entryFileNames: 'static/js/[name]-[hash].js',
        assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
        manualChunks(id) {
          if (id.includes('node_modules')) {
            return 'vendor';
          }
        },
        brotliSize: false, // 不统计
        target: 'esnext',
        minify: 'esbuild' // 混淆器,terser构建后文件体积更小
      },
    },
  }})

三、 入口文件修改

index.html ,全局main.js引入

<script type="module" src="./src/main.js"></script>

 main.js

根据项目已有插件进行升级,注意app创建实例已修改,以下为参考

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from '@/store/index';
import { Modal, message, notification  } from 'ant-design-vue'
import { VueAxios } from './utils/request'
import '@/components/flow/style/flow-designer.scss'
import './components/global.less'
import '@/components/index.less'
import './permission.js' // permission control
import './utils/filter' // global filter
import 'regenerator-runtime/runtime'
import 'core-js/stable'
import socket from './utils/socket'
import { sysApplication } from './utils/applocation'
import { hasBtnPermission } from './utils/permissions' // button permission
import dictionary from './utils/dictionary'

// 引入k-designer表单设计器相关,该设计器适用于antdv3版本
// import 'ant-design-vue/dist/reset.css';
import "k-designer/dist/style.css";
setupAntd(pluginManager);
import { pluginManager } from "k-designer";
import { setupAntd } from "k-designer/dist/ui/antd";
setupAntd(pluginManager);


import * as echarts from 'echarts'
const app = createApp(App)

// 代替Vue2 Vue.use
app.use(router)
app.use(store)
app.use(socket, '/hubs/chatHub')

// 全局注册图标组件
import 'virtual:svg-icons-register'
import * as Icons from '@ant-design/icons-vue'

Object.keys(Icons).map(key => {
  app.component(key, Icons[key])
})


// 代替Vue2 Vue.prototype
app.config.globalProperties.$confirm = Modal.confirm
app.config.globalProperties.$message = message
app.config.globalProperties.$notification = notification
app.config.globalProperties.$info = Modal.info
app.config.globalProperties.$success = Modal.success
app.config.globalProperties.$error = Modal.error
app.config.globalProperties.$warning = Modal.warning
app.config.globalProperties.VueAxios = VueAxios;
app.config.globalProperties.hasPerm = hasBtnPermission;
app.config.globalProperties.applocation = sysApplication;
app.config.globalProperties.dic = dictionary;
app.config.globalProperties.$echarts = echarts;
app.mount('#app')

四、App.vue 及相关<router-view>升级

<router-view class="page" v-slot="{ Component }">
  <transition name="route" mode="out-in">
    <component :is="Component"></component>
  </transition>
</router-view>
<router-view class="page" v-slot="{ Component }">
  <keep-alive>
    <component :is="Component" />
  </keep-alive>
</router-view>

 五、路由

npm install vue-router@4
import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import { constantRouterMap } from '@/config/router.config'
import '@/components/NProgress/nprogress.less' // progress bar custom style


const router = createRouter({
  history: createWebHistory(), // history模式 createWebHashHistory hash模式
  base: import.meta.env.VITE_APP_API_BASE_URL,
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRouterMap  // 路由表
})

// 前增加下面代码,添加一个临时路由
console.log(router.hasRoute(), window.location.pathname)
// if (!router.hasRoute()) {
//   router.addRoute({
//     path: window.location.pathname,
//     name: 'TempRoute',
//     component: () => import('@/views/system/exception/404.vue')
//   })
// }

export default router

六、状态管理VUEX

npm install vuex@next
import { createStore } from 'vuex';
import createPersistedState from 'vuex-persistedstate' // 持久化state, 代替vue-ls相关
export default createStore({  // 替代之前的 new Vuex.Store()
  state: {
  },
  mutations: {
  },
  actions: {
  },
  plugins: [
    createPersistedState({
    storage: window.localStorage //可为window.sessionstorage
  })]
});

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/759063.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C++ 多线程学习总结

C 多线程 创建线程 thread jion与detach方式的区别 jion方式&#xff1a;必须等待创建并启动的子线程任务执行完毕&#xff0c;才会继续往下执行。 示例&#xff1a; #include <stdio.h> #include <unistd.h> #include <iostream> #include <string&g…

【企业架构实践】要避免的 7 个企业架构错误

颠覆性时代需要有弹性、前瞻性的企业架构。不要让错误的框架破坏您的组织实现当前和未来目标的能力。 企业架构为成功的业务 IT 计划奠定了基础。如果设计和实施得当&#xff0c;企业架构将帮助业务领导者实现他们的目标&#xff0c;使组织变得更具响应性、效率和竞争力。 不幸…

matlab使用教程(2)—数组索引、工作区与字符

1数组索引 MATLAB 中的每个变量都是一个可包含许多数字的数组。如果要访问数组的选定元素&#xff0c;请使用索引。 例如&#xff0c;假设有 44 矩阵 A &#xff1a; A [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16] A 44 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 引…

python selenium.webdriver 爬取政策文件

文章目录 获取文章链接批量爬取政策文件应用selenium爬取文件信息数据处理导出为excel 获取文章链接 获取中央人民政府网站链接&#xff0c;进入国务院政策文件库&#xff0c;分为国务院文件和部门文件&#xff08;发改委、工信部、交通运输部、市场监督局、商务部等&#xff…

JavaScript 中 五种迭代数组的方法 every some map filter forEach

JavaScript 中 五种迭代数组的方法 every some map filter forEach 1.every 和 some2.filter (重点常用)3.map (灵活常用&#xff09;4.forEach (重点常用) ECMAScript 提供了 5个对数组的迭代方法 1.every() 2.some() 3.filter() 4.forEach() 5.map() 1.every 和 some every…

串联型PI和并联型PI调节器的比较

一、PI调节器的种类 图3-4 仿真波形变化情况&#xff08;串联型PI调节器&#xff0c;1500r/min&#xff09; 从图3-1到3-4比较可知&#xff0c;与并联型PI调节器相比&#xff0c;串联型PI调节器的超调量很小&#xff08;速度环&#xff09;&#xff0c;且动态过程时间短&…

从新的角度看待大模型微调

一、前言 一切要从最近大火的Lora(《LORA: LOW-RANK ADAPTATION OF LARGE LANGUAGE MODELS》&#xff09;说起&#xff0c;该文章在ICLR2022中提出。说的是利用低秩适配(low-rank adaptation)的方法&#xff0c;可以在使用大模型适配下游任务时只需要训练少量的参数即可达到一…

【SQL应知应会】表分区(二)• MySQL版

欢迎来到爱书不爱输的程序猿的博客, 本博客致力于知识分享&#xff0c;与更多的人进行学习交流 本文收录于SQL应知应会专栏,本专栏主要用于记录对于数据库的一些学习&#xff0c;有基础也有进阶&#xff0c;有MySQL也有Oracle 分区表 • MySQL版 前言一、分区表1.非分区表2.分区…

【运维】第03讲(下):Nginx 负载均衡常见架构及问题解析

Nginx 负载均衡常见问题 那么,Nginx 负载均衡的通常配置会出现哪些问题呢?这里列出几种比较常见的问题: 客户端 IP 地址获取问题域名携带问题负载均衡导致 session 丢失问题动态负载均衡问题真实的 Realserver 状态检测接下来,我们就重点讲解下 Nginx 作为负载均衡的这几个…

从代码角度戳一下springMVC的运行过程-spring16

1、首先页面需要输入地址&#xff1a; 2、画张图------ 这个地址先找Tomcat,而Tomcat帮你找你的工程 3、 4、每次都要过这个地方 这句servlet代码 具体流程是&#xff1a; SpringMVC的执行流程 这个Handler干嘛&#xff1f;负责对你的请求进行解析&#xff0c;知道我最终要找…

WAIC2023丨AI图像内容安全“黑科技”如何助力科技发展?

〇、前言 7月7日下午&#xff0c;2023世界人工智能大会&#xff08;WAIC&#xff09;“聚焦大模型时代AIGC新浪潮—可信AI”论坛在上海世博中心红厅举行。人工智能等技术前沿领域的著名专家与学者、投资人和领军创业者汇聚一堂&#xff0c;共同探索中国科技创新的驱动力量。 在…

latex3【排版】

多行公式排版&#xff1a;&#xff08;gather、align、split、cases&#xff09; \section{多行公式}%gather环境\begin{gather} abba \\ abcbaccbacab\end{gather}\begin{gather*} abba \\ abcbaccbacab\end{gather*}​\begin{gather} abba \\ 123 \notag …

贪吃蛇游戏制作

目录 前言 游戏设计 游戏三部曲 函数说明 优化设计 1 前言 终极目标&#xff1a;打造酷炫贪吃蛇游戏 制作环境: VS2015(支持VC2010,VS各个版本) easyx图形库(稍微改下VC6.0也可以实现) 2 游戏设计 贪吃蛇的制作思路就是蛇头带动蛇尾移动&#xff0c;主要…

记一次linux服务器k8s服务失败,重新安装centos系统并部署k8s以及服务【2023年7月15日】

周五&#xff0c;突然收到微信消息说&#xff1a;兄嘚~ 网站无法访问了&#xff01; 我以为就是普通的小问题&#xff0c;连上服务器看报错呢&#xff0c;执行kubectl get nodes [rootnode101 ~]# kubectl get nodes The connection to the server 127.0.0.1:6443 was refused…

FL Studio 21具有哪些功能?flstudio21会有什么新功能

FL Studio 21 是一个功能完备的音乐制作环境&#xff0c;能够进行多轨道音频录制、音序处理和混音&#xff0c;可以帮助用户创作专业质量的音乐轨道。 借助 VST 托管、灵活的混音器、高级 MIDI 和 ReWire 支持&#xff0c;您将轻松驾驭各种音乐风格。 歌曲或循环可以导出为 .wa…

[QT编程系列-15]: 基础框架 - 信号与槽,connect函数详解

目录 一、线程内不同对象的通信 1.1 connect函数介绍 1.2 connect的用法 1.3 代码示例 二、不同线程间不同对象的通信 2.1 概述 2.2 代码案例 一、线程内不同对象的通信 1.1 connect函数介绍 在使用 Qt 框架进行开发时&#xff0c;connect 函数是用于建立信号与槽机…

hadoop -- Hbase

HBase是一个分布式、可扩展、面向列的数据存储&#xff08;百万级别列&#xff09;、可伸缩、高可靠性、实时读写的NoSQL 数据库。 HBase利用 Hadoop的 HDFS作为其文件存储系统&#xff0c; 利用MapReduce 来处理HBase中的海量数据&#xff0c; 利用Zookeeper作为分布式协同服…

AIGC之文本内容生成概述(下)——Transformer

在上一篇文章中&#xff0c;我们一口气介绍了LSTM、Word2Vec、GloVe、ELMo等四种模型的技术发展&#xff0c;以及每种模型的优缺点与应用场景&#xff0c;全文超过一万字&#xff0c;显得冗长且繁杂&#xff0c;在下文部分我们将分开介绍Transformer、BERT、GPT1/GPT2/GPT3/Cha…

electron+vue3全家桶+vite项目搭建【23】url唤醒应用,并传递参数

文章目录 引入实现效果实现步骤测试代码 引入 demo项目地址 很多场景下我们都希望通过url快速唤醒应用&#xff0c;例如百度网盘&#xff0c;在网页中唤醒应用&#xff0c;并传递下载链接&#xff0c;在electron中要实现这样的效果&#xff0c;就需要针对不同的平台做对应的处…

SpringMVC【SpringMVC拦截器、 SpringMVC跨域请求、SSM整合】(七)-全面详解(学习总结---从入门到深化)

目录 SpringMVC拦截器_拦截器过滤敏感词案例 SpringMVC跨域请求_同源策略 SpringMVC跨域请求_跨域请求 SpringMVC跨域请求_控制器接收跨域请求 SSM整合_需求分析 SSM整合_创建父工程 SSM整合_创建dao子工程 SSM整合_创建service子工程 SSM整合_创建controller子工程 …