全局属性

vue2

  • 对于一些第三方插件,vue2中通常使用prototype原型来挂载到vue对象中
import Vue from 'vue'
Vue.prototype.$http=Axiox
Vue.prototype.$echart= Echart

vue3

  • vue3中提供了一个名为globalProperties的全局属性配置,可以代替vue2中的prototype
app.config.globalProperties.$http = Axios
app.config.globalProperties.$echart = Echart
  • 使用$http
import {getCurrentInstance} from 'vue'

setup () {
const { ctx } = getCurrentInstance();
onMounted(() => {
console.log(ctx.$http)
})
.......
}

ref与v-for的生成

vue2

vue2中v-for与ref一起使用,批量模板引用的时候,获取的ref为一个数组

 <div v-for="i in 3" ref="setItemRef" :key="i">{{i}}</div> //这里是数组

 mounted() {
console.log(this.$refs.setItemRef)
},

vue3

vue3 中ref绑定的是一个函数,

<div v-for="item in 3" :ref="setItemRef"></div> //这里绑定的是函数

setup(){
let itemRefs = []
const setItemRef = el => {
itemRefs.push(el)
}
onMounted(() => {
console.log(itemRefs)
})
}

二者获取ref的dom方式有变化,但是获取的结果相同

异步组件

在路由中,常常使用懒加载的方式来引入组件

vue2

 component: () => import('@/views/homePage/index.vue'),

vue3

在vue3中引入了一个新的方法 --->defineAsyncComponent定义异步组件,来包裹vue2引入方式里面的内容

import { defineAsyncComponent } from 'vue'
component: defineAsyncComponent(() => import('./NextPage.vue'))

自定义指令

改变钩子函数的命名

vue2

vue2中绑定的钩子函数为

  • bind - 指令绑定到元素后发生。只发生一次。
  • inserted - 元素插入父 DOM 后发生。
  • update - 当元素更新,但子元素尚未更新时,将调用此钩子。
  • componentUpdated - 一旦组件和子级被更新,就会调用这个钩子。
  • unbind - 一旦指令被移除,就会调用这个钩子。也只调用一次。

vue3

将钩子函数的命名与生命周期的钩子函数命名相一致

  • bind → beforeMount
  • inserted → mounted
  • beforeUpdate:新的!这是在元素本身更新之前调用的,很像组件生命周期钩子。
  • componentUpdated → updated
  • beforeUnmount:新的!与组件生命周期钩子类似,它将在卸载元素之前调用。
  • unbind -> unmounted

在钩子函数中引用组件实例的方式

某些情况下需要去获取组件中实例的某些属性

vue2

  • 需要通过vnod来获取实例
Vue.directive('has', {
inserted: (el, binding, vnode) => checkPermission(el, binding, vnode),
}); export const checkPermission = (el, binding, vnode) => {
...
const permissionArr = vnode.context.$store.state.permissionId //所拥有的所有权限id
...
}

vue3

  • 从binding中去获取对象
export const checkPermission = (el, binding, vnode) => {
...
const permissionArr =binding.instance.$store.state.permissionId //所拥有的所有权限id
...
}

v-bind="$attrs"

占坑

自定义元素元素的交互

is的用法

vue2

  • 组件:
<component :is="currentTabComponent"></component>
  • html标签
<table>
<tr is="blog-post-row"></tr>
</table>

vue3

  • 组件
<component is="currentTabComponent"></component>
  • html标签
<table>
<tr v-is="'blog-post-row'"></tr> // v-is类似绑定一个变量,而我们需要组件名,为字符串,所以用单引号包裹
</table>

事件

  • vue3中去除了 $on$off$once、三种方法,仅保留$emit

过滤器

vue3中移除了过滤器的功能,建议使用methods或者computed 来代替,实际上在vue2中也完全可以这两种方式实现

局部过滤器

vue2

 <p>{{message|toLower}}</p>

 data() {
return {
message: 'ABC'
}
},
filters: {
toLower(value) {
return value.toLowerCase()
}
}

vue3

  • vue3用computed或者methods来定义
<p>{{messageToLower}}</p>

import {
computed,
ref,
} from 'vue'; setup(){ let message = ref('ABC')
let messageToLower = computed(() => {
// console.log(message)
return message.value.toLowerCase()
})
return{
messageToLower,
} }

全局过滤器

vue2

Vue.filter('toLower',  (value)=> {
return value.toLowerCase()
})

vue3

  • 在main.js中注册
const app =createApp(App)
app.config.globalProperties.$filter={
toLower(value){
return value.toLowerCase()
}
}
  • 使用
<p>{{$filters.toLower(message)}}</p>

根节点

vue2

  • vue2的 template中只能存在一个根节点
<template>
<div id="app">
...
</div>
</template>

vue3

  • vue3中可以存在多个节点
<template>
<div>...</div>
<a>...</a>
<p>...</p>
</template>

函数式组件

占坑

全局API

占坑

内联模板

vue2

  • 利用inline-template属性

  • 在vue2中文档提示了这么一段话,所以几乎没有用过

    不过,inline-template 会让模板的作用域变得更加难以理解。所以作为最佳实践,请在组件内优先选择 template 选项或 .vue 文件里的一个 <template> 元素来定义模板。>

vue3

移除了此功能,

唯一的key

v-if中的key

vue2

  • 在vue2中,v-if,v-else中的key是为了控制某个组件或者元素的复用
  • 不带key的话会复用,< hello-world >组件只创建一次
<!---->
<template v-if="loginType === 'username'">
<hello-world title="username"></hello-world>
</template>
<template v-else>
<hello-world title="email"></hello-world>
</template>
<button @click="changeType">切换</button>
  • 带key的话每次切换都会重新去渲染组件(前提是key不同)
<template v-if="loginType === 'username'">
<hello-world title="username" key="a"></hello-world>
</template>
<template v-else>
<hello-world title="email" key="b"></hello-world>
</template>
<button @click="changeType">切换</button>

vue3

vue3中默认是带有key的,这个key始终保持唯一,与其他的key不同,不能通过故意使用相同的 key 来强制重用分支。

<div v-if="condition">Yes</div>
<div v-else>No</div>

template中v-for的key

vue2

vue2中,在template标签上,可以使用v-for指令,但是不能绑定key,只能在子节点上面去绑定唯一的key

<template v-for="item in list">
<div :key="item.id">...</div>
</template>

vue3

vue3中可以将key绑定到template上

<template v-for="item in list" :key="item.id">
<div>...</div>
</template>

vue3与vue2的区别的更多相关文章

  1. 想知道Vue3与Vue2的区别?五千字教程助你快速上手Vue3!

    从Vue3发布以来,我就一直对其非常感兴趣,就一直想着将其投入公司的生产中,但是开始考虑到很多不确定性就暂时对一些很小的功能进行一些尝试:慢慢的发现组合式Api的形式非常适合开发(个人感觉),尤其是V ...

  2. vue2升级vue3:vue2 vue-i18n 升级到vue3搭配VueI18n v9

    项目从vue2 升级vue3,VueI18n需要做适当的调整.主要是Vue I18n v8.x 到Vue I18n v9 or later 的变化,其中初始化: 具体可以参看:https://vue- ...

  3. vue2.0 与 vue3.0 配置的区别

    提示:要了解vue2.0与vue3.0区别,首先你要熟悉vue2.0 从最明显最简单的开始 项目目录结构 可以明显的看出来,vue2.0与3.0在目录结构方面,有明显的不同(vue3.0我是安装了cs ...

  4. vue2和vue3生命周期的区别

    概念 首先,我们了解一下"生命周期"这个词.通俗的来说,生命周期就是一个事务从出生到消失的过程.例如,一个人从出生到去世.在vue中,vue的生命周期是指,从创建vue对象到销毁v ...

  5. 第三十一篇:vue3和vue2的不同

    好家伙 1.为什么会有vue3? Vue2和Vue3的区别 - 简书 (jianshu.com) 貌似是因为他的对手太优秀,所以他也必须进步 2.什么是api? 从文件操作开始谈API. 以C语言为例 ...

  6. Vue3 相比 vue2

    Vue3 使用Proxy替代了defineProperty. Proxy 相比于 defineProperty 的优势 Object.defineProperty() 的问题主要有三个: 不能监听数组 ...

  7. vue-cli实现异步请求返回mock模拟数据

    在前后端分离开发的过程中,前端开发过程中,页面的数据显示一般都是写死的静态数据,也就是没有经过接口,直接写死在代码中的,在后端给出接口后,再替换为接口数据,为了减少对接成本,mock就出现了.通过预先 ...

  8. Vue2与Vue3的组件通讯对比

    Vue2 父传子 父传子比较简单, 主要通过以下步骤实现 父在template中为子绑定属性 <Child :childData='pMsg'/> <!-- 也可以写死 --> ...

  9. uniapp项目vue2升级vue3简单记录

    看到好多开源项目都升级了vue3,看文章说vue3性能升级很多,而且组合式api很香,遂把最近开发的自助洗车app升级下,在此记录下出现的问题. uniapp升级vue3官方指南 我是先去vue官网看 ...

随机推荐

  1. 2019牛客暑期多校训练营(第十场)F.Popping Balloons(线段树)

    题意:现在给你n个点 现在让你横着划三条线间距为r 然后竖着划三条线间距同样为r 现在让你求经过最多的点数 思路:我们首先建一棵关于y区间的线段树 然后枚举x轴 每次更新重叠的点 然后再更新回去 找一 ...

  2. Codeforces Round #672 (Div. 2)

    比赛链接:https://codeforces.com/contest/1420 A. Cubes Sorting 题意 给出一个大小为 $n$ 的数组 $a$,每次只可以交换相邻的两个元素,最多交换 ...

  3. 2017-2018 ACM-ICPC, Asia Daejeon Regional Contest PART(10/12)

    $$2017-2018\ ACM-ICPC,\ Asia\ Daejeon\ Regional\ Contest$$ \(A.Broadcast\ Stations\) \(B.Connect3\) ...

  4. 【bzoj 3333】排队计划(线段树)

    n个数,求一次逆序对.接着有m次修改操作,把每次输入的位置p的数之后<=它的数取出来,从小到大排序后再放回空位里,求逆序对.(N,M<=500,000 , Ai<=10^9)思路:1 ...

  5. hdu1263 水果

    Problem Description 夏天来了~~好开心啊,呵呵,好多好多水果~~ Joe经营着一个不大的水果店.他认为生存之道就是经营最受顾客欢迎的水果.现在他想要一份水果销售情况的明细表,这样J ...

  6. Codeforces Round #479 (Div. 3) C. Less or Equal (排序,贪心)

    题意:有一个长度为\(n\)的序列,要求在\([1,10^9]\)中找一个\(x\),使得序列中恰好\(k\)个数满足\(\le x\).如果找不到\(x\),输出\(-1\). 题解:先对这个序列排 ...

  7. B - 来找一找吧 HihoCoder - 1701

    题目: 这次到渣渣问桶桶了... 准备给你n个数a1, a2, ... an,桶桶你能从中找出m个特别的整数吗,我想让任意两个之差都是k的倍数. 请你计算有多少种不同的选法.由于选法可能非常多,你只需 ...

  8. 远程连接 出现身份验证错误,要求的函数不受支持(这可能是由于CredSSP加密Oracle修正)

    修改本地组策略: 计算机配置>管理模板>系统>凭据分配>加密Oracle修正 选择启用并选择"易受攻击". 原文:https://blog.csdn.net ...

  9. Databricks 第11篇:Spark SQL 查询(行转列、列转行、Lateral View、排序)

    本文分享在Azure Databricks中如何实现行转列和列转行. 一,行转列 在分组中,把每个分组中的某一列的数据连接在一起: collect_list:把一个分组中的列合成为数组,数据不去重,格 ...

  10. mysql-画图

    目录 阿里数据库产品rds 淘宝数据库架构 数据库下载 Mysql3种安装方法 mysql_install_db安装数据库命令脚本中有生成初始mysql数据 也可以把mysql_install_db集 ...