《Vue3 基础知识》 使用 GoGoCod 升级到Vue3+ElementPlus 适配处理

此篇为 《Vue2+ElementUI 自动转 Vue3+ElementPlus(GoGoCode)》 的扩展!

Vue3 适配

Vue3 不兼容适配

Vue 3 迁移指南 在此,本章只讲述项目或组件库中遇到的问题;

  1. Vue3 移除 o n , on, on,off 和 $once 实例方法,事件总线Bus的差异;

  2. Vue3 CSS 深度选择器 有变化,有警告[@vue/compiler-sfc] the >>> and /deep/ combinators have been deprecated. Use :deep() instead.

    css:line-numbers 复制代码
    /* Vue2 写法一 */
    .a >>>.b
    /* Vue2 写法二 */
    .a /deep/ .b
    
    /* Vue3 写法*/
    .a :deep(.b) 
  3. Vue props 多个类型,不要用 | ,用数组 \[\]。否则报错 expected "indent", got "eos"

    js:line-numbers 复制代码
    expandLevel: {
        // type: Number | String,// vue2 可以,vue3 报错
        type: [Number, String], // vue2/vue3 正确
        default: 1,
    }

GoGoCode 自动升级适配

  1. v-model:value 报错,全局搜 v-model:value 并全局替换为 v-model。其实 Vue2 和 Vue3 都不加 :value,不知为何转换时加上了...

  2. 插槽报错Duplicate slot names found 发现重复的插槽名称 。仅两个 slot 在一块报错...

  3. HTML 元素上的方法,例如 @click= 中有多个表达式仅换行没分号 ; ,这是语法错误。建议多个表达式就写在方法里

Webpack 转 Vite 适配

动态加载文件 Webpack 用 require.context, Vite 用 import.meta.glob

  • src\params.js
  • src\store\index.js
  • src\components\onemap\func.js 注意这里是异步加载,否则初始化最先加载,找不到 app 对象

Webpack 用 require.context

js 复制代码
const modulesList = require.context("./src/components", true, /\.vue$/);
const modules = modulesList.keys().reduce((obj, modulePath) => {
   // 文件名
      const moduleName = modulePath.replace(/^\.\/(.*)\/(.*)\.\w+$/, "$2");
      // 模块对象
      let moduleObj = modulesList(modulePath);
      // 放入模块
      obj[moduleName] = moduleObj.default;
      return obj;
   }, {});

Vite 用 import.meta.glob

js:line-numbers 复制代码
// 注意加在 `eager: true` 是同步处理
const modulesList = import.meta.glob('./src/components/**/*.vue', { eager: true });
const modules = Object.keys(modulesList).reduce((obj, path) => {
   // 文件名
   const moduleName = path.replace(/(.*\/)*([^.]+).*/ig, "$2");
   // 放入模块
   obj[moduleName] = modulesList[path].default;
   return obj;
}, {})
js 复制代码
const modulesFiles = import.meta.glob('./funcs/**/*.vue')

let modules = {};
for (const path in modulesFiles) {
    modulesFiles[path]().then((mod) => {
      // 文件名
      const moduleName = path.replace(/(.*\/)*([^.]+).*/ig, "$2");
      // 放入模块
        modules[moduleName] = mod.default;
    })
}

Element Plus 适配

  1. el-button 警告 [props] [API] type.text is about to be deprecated in version 3.0.0, please use link instead.

解决: 官网中 el-button type="text" 用于链接按钮已在 v3.0.0 废除

html 复制代码
// 原
<el-button type="text">文字按钮</el-button>

// 改为
<el-button type="primary" link>文字按钮</el-button>

  1. el-input 警告 Invalid prop: validation failed. Expected one of ["", "default", "small", "large"], got value "mini".

解决: 属性 size 在 ElementUI 和 ElementPlus 之间有差异


  1. el-tabs 方法 tab-click 返回值 ElementUI 和 ElementPlus 不一样

差异: ElementUI el-tabs 和 ElementPlus el-tabs


  1. el-input ElementUI 和 ElementPlus 有点差异,ElementPlus 多嵌套了一层 el-input__wrapper

解决: 改造 src\assets\scss\elements\input.scss,设置 padding:0 !important


  1. Element Icon 图标 警告 voided by marking the component with markRawor usingshallowRefinstead ofref.

解决: 以转换后一张图系统文件 src\components\onemap\mode-refreshing\head.vue 为例。代码第 10 行 ElIconSetting,放在 data() 中,做了深度响度,会有必要的开销。

js:line-numbers 复制代码
// 修改前
import {
  Setting as ElIconSetting,
  DataAnalysis as ElIconDataAnalysis,
  SwitchButton as ElIconSwitchButton,
} from '@element-plus/icons-vue'
export default {
  data() {
    return {
      ElIconSetting,
      ElIconDataAnalysis,
      ElIconSwitchButton
    }
  },
}

提示加上 markRaw 不被代理 或 shallowRef 浅层响应 。如代码 2,11-13 行

js:line-numbers 复制代码
// 修改后
import { shallowRef } from 'vue'
import {
  Setting as ElIconSetting,
  DataAnalysis as ElIconDataAnalysis,
  SwitchButton as ElIconSwitchButton,
} from '@element-plus/icons-vue'
export default {
  data() {
    return {
      ElIconSetting: shallowRef(ElIconSetting),
      ElIconDataAnalysis: shallowRef(ElIconDataAnalysis),
      ElIconSwitchButton: shallowRef(ElIconSwitchButton),
    }
  },
}
相关推荐
小郎君6583 小时前
(一)为什么表单需要第三种状态:disabled、readonly 与纯文本展示
前端·vue.js
jktantan6 小时前
(二)Vue 3 的 attrs 不是响应式:el-form 扩展属性同步的三种方案
前端·javascript·vue.js·element-plus
Highcharts.js9 小时前
Highcharts 集成到 React 或 Vue 项目中时,如何高效地使用 TypeScript技巧?
vue.js·人工智能·react.js·typescript·前端框架·highcharts
Java后端的Ai之路18 小时前
CAS并发控制-从CPU指令到企业级乐观锁
vue.js·人工智能·python·cas·乐观锁
jktantan1 天前
每个节点 41 字节:把一棵百万节点树拆进 14 个 typed array
前端·javascript·vue.js·算法·typescript
小大宇1 天前
Vue知识点-ElementPlus-06 单选+多选+下拉+日期选择
前端·vue.js·elementui
小大宇1 天前
Vue知识点-ElementPlus-05 输入框
前端·javascript·vue.js
软件技术NINI1 天前
基于SpringBoot和Vue的摇滚乐鉴赏网站
vue.js·spring boot·后端·spring·html·毕业设计·毕设
计算机毕设定制辅导-无忧学长1 天前
《基于物品协同过滤的闲置书换购微信小程序设计开发》
java·vue.js·spring boot·微信小程序·uniapp·闲置书换购微信小程序·物品协同过滤算法
liuchangng1 天前
React 19 + Vite 6 实战:从教材 PDF 到 866 词间隔重复单词 App
前端·react.js·pdf·vite·开源项目