当前位置: 首页 > article >正文

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

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

Vue3 适配

Vue3 不兼容适配

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

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

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

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

    expandLevel: {
        // type: Number | String,// vue2 可以,vue3 报错
        type: [Number, String], // vue2/vue3 正确
        default: 1,
    }
    

GoGoCode 自动升级适配

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

    在这里插入图片描述

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

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

Webpack 转 Vite 适配

动态加载文件 Webpackrequire.context, Viteimport.meta.glob

  • src\params.js
  • src\store\index.js

Webpackrequire.context

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;
   }, {});

Viteimport.meta.glob

// 注意加在 `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;
}, {})
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 废除

// 原
<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".

解决: 属性 sizeElementUIElementPlus 之间有差异

  • Element UI 用 medium / small / mini
  • Element Plus 用 large / default / small

  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\mode-refreshing\head.vue 为例。代码第 10ElIconSetting,放在 data() 中,做了深度响度,会有必要的开销。

// 修改前
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

// 修改后
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),
    }
  },
}

http://www.kler.cn/a/227482.html

相关文章:

  • 线程和进程的区别及基础线程创建
  • 为什么说TiDB在线扩容对业务几乎没有影响
  • c语言:贪吃蛇的实现
  • 知识图谱推理方法综述
  • 适用于嵌入式单片机的压缩算法
  • 使用gcc/g++查看C语言预处理,编译,汇编,连接,以及动静态库的区分
  • Redis 哨兵(Sentinel)
  • Elastic Search 6.x 版本 rollover 配置
  • 探索前端开发框架:React、Angular 和 Vue 的对决(一)
  • 数据结构中的时间复杂度和空间复杂度基础
  • android tv开发-1,leanback
  • 稀疏场景高性能训练方案演变|京东广告算法架构体系最佳实践
  • C语言内存分配函数知识汇总
  • Android 高德地图切换图层
  • Python实现设计模式-策略模式
  • 【Soc级系统防御】基于IP的SoC设计中的安全问题
  • 通过html2canvas和jsPDF将网页内容导出成pdf
  • 想要远程云打印怎么办?如何实现远程云打印?
  • MACos虚拟机安装全过程
  • MAC word删除空白页