Vue2升级Vue3填坑笔记

news/2024/9/13 23:13:52/ 标签: 前端, vue.js, javascript

在这里插入图片描述

背景

前段时间使用Vue2完成一个流量回放的前端开发,实现了流量回放的基本功能。开发过程中,发现现主流的插件都在适配Vue3,奈何为了赶进度,只能先用自己熟悉的Vue2先顶上。恰巧最近有些许空余时间,就把项目代码逐步变更Vue3了。过程中,顺便梳理了下遇到的差异,为后面做些许积累与经验参考。

差异处理

element常见差异:

  1. deleteset在vue3中不再使用,可以直接将对应的值设置为null

  2. router引入:

    • Vue2
      import Router from 'vue-router' 
      
    • Vue3
      import {createRouter, createWebHistory} from "vue-router"
      
  3. 使用router的history功能,需要添加属性, 指定路径

    import {createRouter, createWebHistory} from "vue-router";
    const router = createRouter({history: createWebHistory('/'),routes : []})
    export default router
    
  4. 从url中获取参数。在 Vue 2 中,$route 是一个全局属性,但在 Vue 3 中它已被弃用,取而代之的是 useRoute()

    • vue2
      this.recordId = this.$router.history.current.params.id
      
    • vue3
      import { useRoute } from 'vue-router';const route = useRoute();
      this.collectionId = route.params.id;
      console.log(`录制任务ID:${this.collectionId}`);
      
  5. 处理eslint声明后未使用的报错(vue3解决no-unused-vars报错),修改eslintConfig规则

    {     "eslintConfig": {"root": false,"env": {"node": true},"extends": ["plugin:vue/vue3-essential","eslint:recommended"],"parserOptions": {"parser": "@babel/eslint-parser"},"rules": {"no-unused-vars": ["error",{"varsIgnorePattern": ".*","args": "none"}]}},"browserslist": ["> 1%","last 2 versions","not dead","not ie 11"]
    }
  6. :visible.sync 变为 v-model

  7. 自定义按钮弹出框内容:
    在这里插入图片描述

    • Vue2
      <el-popconfirmtitle="是否确定删除?"confirm-button-text="确定"cancel-button-text="取消"@confirm="deleteCollectData(scope.row.id)"
      >
      <el-button slot="reference" type="text" size="small">删除
      </el-button>
      </el-popconfirm>
      
    • Vue3
      <el-popconfirmtitle="是否确定删除?"confirm-button-text="确定"cancel-button-text="取消"@confirm="deleteCollectData(scope.row.id)"
      > <template #reference><el-button type="primary" size="small">删除</el-button></template>
      
  8. DatePicker

    在这里插入图片描述

    • vue2:

      <el-date-pickerv-model="form.executionTime"type="datetime"format="yyyy-MM-dd HH:mm:ss"value-format="yyyy-MM-dd HH:mm:ss"placeholder="选择日期时间":picker-options="pickerOptionsStart"
      />
      
    • vue3:

      <el-date-pickerv-model="form.executionTime"type="datetime"format="YYYY-MM-DD HH:mm:ss"value-format="YYYY-MM-DD HH:mm:ss"placeholder="选择日期时间"
      />
      
  9. el-table表格数据中的取值方式:

    • vue2
      <el-table :data="tableData" style="width: 100%"><el-table-column label="Date" width="180">----------- <template slot-scope="scope"> -----------<div style="display: flex; align-items: center"><el-icon><timer /></el-icon><span style="margin-left: 10px">{{ scope.row.date }}</span></div></template> </el-table-column>
      </el-table>
      
    • vue3
      <el-table :data="tableData" style="width: 100%"><el-table-column label="Date" width="180">----------- <template #default="scope"> -----------<div style="display: flex; align-items: center"><el-icon><timer /></el-icon><span style="margin-left: 10px">{{ scope.row.date }}</span></div></template> </el-table-column>
      </el-table>
      
  10. 表格数据过滤条件
    在这里插入图片描述

    • vue2
      <template slot="header" slot-scope="scope"></template>
      
    • vue3
      <template #header></template>
      
  11. :value.sync页面取值为变量

    • vue2

      <my-component :value.sync="data"></my-component>
      
    • vue3

      <my-component v-model:data="data"></my-component>
      
  12. el-link组件在vue2中,需要使用#:
    <el-link type="primary" :href="'#/replay/jobDetail/'+replay.taskId">{{ replay.taskName }}</el-link>
    在vue3中:
    <el-link type="primary" :href="'/replay/jobDetail/'+replay.taskId">{{ replay.taskName }}</el-link>

  13. slot= Vue2、Vue3中的差异 footer、extra

    • vue2

      <div slot="footer" class="dialog-footer"></div>
      
    • vue3

      <template #footer><div class="dialog-footer"></div>
      </template>
      
    • vue2

      <el-descriptions class="margin-top" title="任务配置" :column="3"><template slot="extra"></template>
      </el-descriptions>
      
    • vue3

      <el-descriptions class="margin-top" title="任务配置" :column="3"><template #extra></template>
      </el-descriptions>
      
  14. 异常处理:

    1. Invalid prop: validation failed. Expected one of ["", "default", "small", "large"], got value "mini".
      修改vue2中的size='mini'size='small'

    2. element-plus type.text is about to be deprecated in version 3.0.0, please use link instead.
      element-button的type='text'属性已移除。

    3. ResizeObserver loop completed with undelivered notifications 解决改报错,需要修改app.vuemain.js两个文件:

      • app.vue:

        <template>
        <!--  <img alt="Vue logo" src="./assets/logo.png">-->
        <!--  <HelloWorld msg="Welcome to Your Vue.js App"/>--><main><RouterView /></main>
        </template><script>
        // app.vue写在script里面  main.js写在app挂在完之后
        const debounce = (fn, delay) => {let timerreturn (...args) => {if (timer) {clearTimeout(timer)}timer = setTimeout(() => {fn(...args)}, delay)}
        }const _ResizeObserver = window.ResizeObserver;
        window.ResizeObserver = class ResizeObserver extends _ResizeObserver{constructor(callback) {callback = debounce(callback, 200);super(callback);}
        }</script><style>
        #app {font-family: Avenir, Helvetica, Arial, sans-serif;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;text-align: center;color: #2c3e50;margin-top: 60px;
        }
        </style>
      • main.js:

        import {createApp} from 'vue'const app = createApp(App)
        app.mount('#app')// app.vue写在script里面  main.js写在app挂在完之后
        const debounce = (fn, delay) => {let timerreturn (...args) => {if (timer) {clearTimeout(timer)}timer = setTimeout(() => {fn(...args)}, delay)}
        }const _ResizeObserver = window.ResizeObserver;
        window.ResizeObserver = class ResizeObserver extends _ResizeObserver{constructor(callback) {callback = debounce(callback, 200);super(callback);}
        }
        
    4. vcrontab 只适用于vue2,在vue3中可以使用vue3-cron-plus no-vue3-cron 参考之前分享博客

    5. vue-jsonpath-picker只适用于Vue2,在Vue3中可以使用vue-json-pretty 参考之前分享博客


http://www.ppmy.cn/news/1515122.html

相关文章

芯片要火不要“热”!仿真技术助力芯片热设计

芯片散热仿真好比一场微观世界里的“清凉大作战”&#xff01; 想象一下&#xff0c;小小的芯片就像迷你城市&#xff0c;无数的电子如同居民在其中穿梭。当芯片高速运转&#xff0c;就像城市进入了狂欢&#xff0c;热闹非凡但也会产生大量的热量。 而芯片散热仿真用数字和算法…

电子电气架构--- 智能汽车电子架构的核心诉求

我是穿拖鞋的汉子&#xff0c;魔都中坚持长期主义的汽车电子工程师。 老规矩&#xff0c;分享一段喜欢的文字&#xff0c;避免自己成为高知识低文化的工程师&#xff1a; 屏蔽力是信息过载时代一个人的特殊竞争力&#xff0c;任何消耗你的人和事&#xff0c;多看一眼都是你的不…

【Material-UI】Radio Group中的 Color 属性详解

文章目录 一、Radio Group 组件概述1. 组件介绍2. 基本用法 二、Color 属性详解1. Color 属性的作用2. 使用 Color 属性设置颜色3. 自定义颜色 三、Color 属性的实际应用场景1. 品牌一致性2. 状态指示3. 突出特定选项 四、注意事项1. 色彩对比2. 无障碍设计3. 主题定制 五、总结…

Swift 中的影像魔术:Core Video 的高级应用

标题&#xff1a;Swift 中的影像魔术&#xff1a;Core Video 的高级应用 在 Swift 开发中&#xff0c;Core Video 是 Apple 提供的一个强大的框架&#xff0c;用于处理高质量的视频内容。从实时视频滤镜到高级图像处理&#xff0c;Core Video 为开发者提供了丰富的 API 来实现…

wpf VisualStateManager.VisualStateGroups 介绍和举例

VisualStateManager.VisualStateGroups 是 WPF (Windows Presentation Foundation) 和 UWP (Universal Windows Platform) 中用于控制 UI 元素在不同状态&#xff08;如鼠标悬停、选中、未激活等&#xff09;下视觉表现的一个机制。它通过定义一系列的视觉状态&#xff08;Visu…

前端宝典十三:node网络详解Tcp/IP/Http及网络安全防御

讨论网络相关的问题前&#xff0c;我们首先看一下从浏览器输入 URL 到显示前端页面的流程&#xff0c;首先从TCP的应用层、传输层、网络层、数据链路层开始看&#xff1a; 一、应用层、传输层、网络层、数据链路层 以下是从浏览器输入 URL 到显示前端页面的流程顺序解析&…

自动化运维(ansible---playbook)

playbook(剧本): 是ansible用于配置,部署,和管理被控节点的剧本。用于ansible操作的编排。 参考:https://docs.ansible.com/ansible/latest/user_guide/playbooks _intro.html 使用的格式为yaml格式&#xff08;saltstack,elk,docker,docker-compose,kubernetes等也都会用到y…

Linux(面试篇)

目录 什么是Linux 什么是Linux内核&#xff1f; Linux的基本组件是什么&#xff1f; Bash和Dos之间基本区别是什么&#xff1f; 什么是Root账户 什么是Bash? 什么时CLI? Linux的目录结构时怎样的&#xff1f; 什么是硬链接和软链接&#xff1f; 什么叫CC攻击&#…

动漫二次元漂亮的网站导航HTML5源码

二次元漂亮网站导航HTML源码&#xff0c;页面中还调用了很多外站的图片等链接需自行更换。 动漫二次元漂亮的网站导航HTML5源码

简易版营业厅宽带系统

TOC ssm018简易版营业厅宽带系统jsp 绪论 1.1 研究背景 当前社会各行业领域竞争压力非常大&#xff0c;随着当前时代的信息化&#xff0c;科学化发展&#xff0c;让社会各行业领域都争相使用新的信息技术&#xff0c;对行业内的各种相关数据进行科学化&#xff0c;规范化管…

数据结构day01(数据结构、算法基础知识)

目录 【1】数据结构基础知识 1》什么是数据结构 2》数据 3》逻辑结构 1>线性关系 2>层次关系 3>网状关系 4》存储结构 1>顺序存储 2>链式存储 3>索引存储结构 4>散列存储 5》操作 【2】算法基础知识 1> 什么是算法 2> 算法设计 3> 算…

五、Centos7-安装Jenkins--吃灰去吧

克隆了一个base的虚拟机&#xff0c;用来安装Jenkins 2023年11月&#xff0c;Jenkins不支持centos7了。我们只是学习用&#xff0c;先看看吧。 &#xff08; 另一个人用别的操作系统安装的jenkins&#xff0c;可以参考 版权声明&#xff1a;本文为博主原创文章&#xff0c;…

卓越测试工程师必备:团队协作的艺术

引言 在快速发展的软件行业,团队协作已成为推动项目成功的关键因素之一。作为测试工程师,不仅需要具备扎实的技术基础,还需要具备出色的团队协作能力。本文将探讨团队协作的重要性,并分享一些培养这方面能力的方法。 一、团队协作的重要性 1. 提升效率 在团队中,成员可以…

16.C基础_内存管理

内存分区 1、整体框图 内存分为代码区、全局区、栈区、堆区。代码区和全局区在代码编译完之后就已经确定&#xff0c;栈区和堆区是在程序运行时进行开辟和释放的。整体内存分区框图如下&#xff1a; 对于一个进程&#xff0c;它一共有4G的空间&#xff0c;其中0~3G为上述的4个…

CSS有趣知识

4.圆角边框 在CSS3中&#xff0c;新增了圆角边框样式&#xff0c;这样我们的盒子就可以变圆角了。 border-radius 属性用于设置元素的外边框圆角。 语法&#xff1a; border-radius:length; radius 半径(圆的半径)原理&#xff1a;(椭圆)与边框的交集形成圆角效果 1.参数值可以…

springboot功能模块之POI操作Excel

一、前言 文件的导入导出: 这个功能主要就是帮助我们的用户能够快速的将数据导入到数据库中,不用在自己手动的一条一条的将数据新增到我们的数据库中.同时又能够方便我们能够将数据导出之后打印出来给领导们查看.不用非得带着电脑这里那里的跑.非常实用的功能. 文件的导入导…

Appium学习

一、基础配置 import unittest from appium import webdriver from appium.options.android import UiAutomator2Options from appium.webdriver.common.appiumby import AppiumBy from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support …

SpringBoot集成kafka接收对象消息

SpringBoot集成kafka接收对象消息 1、生产者2、消费者3、工具类4、消息实体对象5、配置文件6、启动类7、测试类8、测试结果 1、生产者 package com.power.producer;import com.power.model.User; import com.power.util.JSONUtils; import org.springframework.kafka.core.Kaf…

YOLOv10:实时端到端目标检测

摘要 https://arxiv.org/pdf/2405.14458 近年来&#xff0c;YOLO系列模型因其在计算成本与检测性能之间的有效平衡&#xff0c;在实时目标检测领域占据了主导地位。研究人员在YOLO的架构设计、优化目标、数据增强策略等方面进行了探索&#xff0c;并取得了显著进展。然而&…

2408gui,学习gui的经验

// 对话 主标 DIALOGEX 0, 0, 100,100 STYLE DS_SHELLFONT|DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "动作" FONT 12, "MS Sans Serif" BEGIN//ICON 翻译标,IDC_STATIC,0,0,20,20DEFPUSHBUTTON "开始",开…