1.Vue3簡(jiǎn)介
- 2020年9月18日筷凤,Vue.js發(fā)布3.0版本饺谬,代號(hào):One Piece(海賊王)
- 耗時(shí)2年多逼裆、2600+次提交、30+個(gè)RFC凰浮、600+次PR凭峡、99位貢獻(xiàn)者
- github上的tags地址:https://github.com/vuejs/vue-next/releases/tag/v3.0.0
2.Vue3帶來(lái)了什么
1.性能的提升
打包大小減少41%
初次渲染快55%, 更新渲染快133%
-
內(nèi)存減少54%
......
2.源碼的升級(jí)
使用Proxy代替defineProperty實(shí)現(xiàn)響應(yīng)式
-
重寫(xiě)虛擬DOM的實(shí)現(xiàn)和Tree-Shaking
......
3.擁抱TypeScript
- Vue3可以更好的支持TypeScript
4.新的特性
-
Composition API(組合API)
- setup配置
- ref與reactive
- watch與watchEffect
- provide與inject
- ......
-
新的內(nèi)置組件
- Fragment
- Teleport
- Suspense
-
其他改變
- 新的生命周期鉤子
- data 選項(xiàng)應(yīng)始終被聲明為一個(gè)函數(shù)
- 移除keyCode支持作為 v-on 的修飾符
- ......
一拙已、創(chuàng)建Vue3.0工程
1.使用 vue-cli 創(chuàng)建
官方文檔:https://cli.vuejs.org/zh/guide/creating-a-project.html#vue-create
## 查看@vue/cli版本,確保@vue/cli版本在4.5.0以上
vue --version
## 安裝或者升級(jí)你的@vue/cli
npm install -g @vue/cli
## 創(chuàng)建
vue create vue_test
## 啟動(dòng)
cd vue_test
npm run serve
2.使用 vite 創(chuàng)建
官方文檔:https://v3.cn.vuejs.org/guide/installation.html#vite
vite官網(wǎng):https://vitejs.cn
- 什么是vite摧冀?—— 新一代前端構(gòu)建工具倍踪。
- 優(yōu)勢(shì)如下:
- 開(kāi)發(fā)環(huán)境中,無(wú)需打包操作索昂,可快速的冷啟動(dòng)建车。
- 輕量快速的熱重載(HMR)。
- 真正的按需編譯椒惨,不再等待整個(gè)應(yīng)用編譯完成缤至。
- 傳統(tǒng)構(gòu)建 與 vite構(gòu)建對(duì)比圖
<img src="https://cn.vitejs.dev/assets/bundler.37740380.png" style="width:500px;height:280px;float:left" /><img src="https://cn.vitejs.dev/assets/esm.3070012d.png" style="width:480px;height:280px" />
## 創(chuàng)建工程
npm init vite-app <project-name>
## 進(jìn)入工程目錄
cd <project-name>
## 安裝依賴(lài)
npm install
## 運(yùn)行
npm run dev
二、常用 Composition API
官方文檔: https://v3.cn.vuejs.org/guide/composition-api-introduction.html
1.拉開(kāi)序幕的setup
- 理解:Vue3.0中一個(gè)新的配置項(xiàng)康谆,值為一個(gè)函數(shù)领斥。
- setup是所有<strong style="color:#DD5145">Composition API(組合API)</strong><i style="color:gray;font-weight:bold">“ 表演的舞臺(tái) ”</i>。
- 組件中所用到的:數(shù)據(jù)沃暗、方法等等月洛,均要配置在setup中。
- setup函數(shù)的兩種返回值:
- 若返回一個(gè)對(duì)象孽锥,則對(duì)象中的屬性嚼黔、方法, 在模板中均可以直接使用。(重點(diǎn)關(guān)注3腊取)
- <span style="color:#aad">若返回一個(gè)渲染函數(shù):則可以自定義渲染內(nèi)容隔崎。(了解)</span>
- 注意點(diǎn):
- 盡量不要與Vue2.x配置混用
- Vue2.x配置(data、methos韵丑、computed...)中<strong style="color:#DD5145">可以訪問(wèn)到</strong>setup中的屬性、方法虚缎。
- 但在setup中<strong style="color:#DD5145">不能訪問(wèn)到</strong>Vue2.x配置(data撵彻、methos钓株、computed...)。
- 如果有重名, setup優(yōu)先陌僵。
- setup不能是一個(gè)async函數(shù)轴合,因?yàn)榉祷刂挡辉偈莚eturn的對(duì)象, 而是promise, 模板看不到return對(duì)象中的屬性。(后期也可以返回一個(gè)Promise實(shí)例碗短,但需要Suspense和異步組件的配合)
- 盡量不要與Vue2.x配置混用
2.ref函數(shù)
- 作用: 定義一個(gè)響應(yīng)式的數(shù)據(jù)
- 語(yǔ)法:
const xxx = ref(initValue)
- 創(chuàng)建一個(gè)包含響應(yīng)式數(shù)據(jù)的<strong style="color:#DD5145">引用對(duì)象(reference對(duì)象受葛,簡(jiǎn)稱(chēng)ref對(duì)象)</strong>。
- JS中操作數(shù)據(jù):
xxx.value
- 模板中讀取數(shù)據(jù): 不需要.value偎谁,直接:
<div>{{xxx}}</div>
- 備注:
- 接收的數(shù)據(jù)可以是:基本類(lèi)型总滩、也可以是對(duì)象類(lèi)型。
- 基本類(lèi)型的數(shù)據(jù):響應(yīng)式依然是靠
Object.defineProperty()
的get
與set
完成的巡雨。 - 對(duì)象類(lèi)型的數(shù)據(jù):內(nèi)部 <i style="color:gray;font-weight:bold">“ 求助 ”</i> 了Vue3.0中的一個(gè)新函數(shù)——
reactive
函數(shù)闰渔。
3.reactive函數(shù)
- 作用: 定義一個(gè)<strong style="color:#DD5145">對(duì)象類(lèi)型</strong>的響應(yīng)式數(shù)據(jù)(基本類(lèi)型不要用它,要用
ref
函數(shù)) - 語(yǔ)法:
const 代理對(duì)象= reactive(源對(duì)象)
接收一個(gè)對(duì)象(或數(shù)組)铐望,返回一個(gè)<strong style="color:#DD5145">代理對(duì)象(Proxy的實(shí)例對(duì)象冈涧,簡(jiǎn)稱(chēng)proxy對(duì)象)</strong> - reactive定義的響應(yīng)式數(shù)據(jù)是“深層次的”。
- 內(nèi)部基于 ES6 的 Proxy 實(shí)現(xiàn)正蛙,通過(guò)代理對(duì)象操作源對(duì)象內(nèi)部數(shù)據(jù)進(jìn)行操作督弓。
4.Vue3.0中的響應(yīng)式原理
vue2.x的響應(yīng)式
-
實(shí)現(xiàn)原理:
對(duì)象類(lèi)型:通過(guò)
Object.defineProperty()
對(duì)屬性的讀取、修改進(jìn)行攔截(數(shù)據(jù)劫持)乒验。-
數(shù)組類(lèi)型:通過(guò)重寫(xiě)更新數(shù)組的一系列方法來(lái)實(shí)現(xiàn)攔截愚隧。(對(duì)數(shù)組的變更方法進(jìn)行了包裹)。
Object.defineProperty(data, 'count', { get () {}, set () {} })
-
存在問(wèn)題:
- 新增屬性徊件、刪除屬性, 界面不會(huì)更新奸攻。
- 直接通過(guò)下標(biāo)修改數(shù)組, 界面不會(huì)自動(dòng)更新。
Vue3.0的響應(yīng)式
- 實(shí)現(xiàn)原理:
- 通過(guò)Proxy(代理): 攔截對(duì)象中任意屬性的變化, 包括:屬性值的讀寫(xiě)虱痕、屬性的添加睹耐、屬性的刪除等。
- 通過(guò)Reflect(反射): 對(duì)源對(duì)象的屬性進(jìn)行操作部翘。
- MDN文檔中描述的Proxy與Reflect:
Proxy:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Proxy
-
Reflect:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Reflect
new Proxy(data, { // 攔截讀取屬性值 get (target, prop) { return Reflect.get(target, prop) }, // 攔截設(shè)置屬性值或添加新屬性 set (target, prop, value) { return Reflect.set(target, prop, value) }, // 攔截刪除屬性 deleteProperty (target, prop) { return Reflect.deleteProperty(target, prop) } }) proxy.name = 'tom'
5.reactive對(duì)比ref
- 從定義數(shù)據(jù)角度對(duì)比:
- ref用來(lái)定義:<strong style="color:#DD5145">基本類(lèi)型數(shù)據(jù)</strong>硝训。
- reactive用來(lái)定義:<strong style="color:#DD5145">對(duì)象(或數(shù)組)類(lèi)型數(shù)據(jù)</strong>。
- 備注:ref也可以用來(lái)定義<strong style="color:#DD5145">對(duì)象(或數(shù)組)類(lèi)型數(shù)據(jù)</strong>, 它內(nèi)部會(huì)自動(dòng)通過(guò)
reactive
轉(zhuǎn)為<strong style="color:#DD5145">代理對(duì)象</strong>新思。
- 從原理角度對(duì)比:
- ref通過(guò)
Object.defineProperty()
的get
與set
來(lái)實(shí)現(xiàn)響應(yīng)式(數(shù)據(jù)劫持)窖梁。 - reactive通過(guò)使用<strong style="color:#DD5145">Proxy</strong>來(lái)實(shí)現(xiàn)響應(yīng)式(數(shù)據(jù)劫持), 并通過(guò)<strong style="color:#DD5145">Reflect</strong>操作<strong style="color:orange">源對(duì)象</strong>內(nèi)部的數(shù)據(jù)。
- ref通過(guò)
- 從使用角度對(duì)比:
- ref定義的數(shù)據(jù):操作數(shù)據(jù)<strong style="color:#DD5145">需要</strong>
.value
夹囚,讀取數(shù)據(jù)時(shí)模板中直接讀取<strong style="color:#DD5145">不需要</strong>.value
纵刘。 - reactive定義的數(shù)據(jù):操作數(shù)據(jù)與讀取數(shù)據(jù):<strong style="color:#DD5145">均不需要</strong>
.value
。
- ref定義的數(shù)據(jù):操作數(shù)據(jù)<strong style="color:#DD5145">需要</strong>
6.setup的兩個(gè)注意點(diǎn)
-
setup執(zhí)行的時(shí)機(jī)
- 在beforeCreate之前執(zhí)行一次荸哟,this是undefined假哎。
-
setup的參數(shù)
- props:值為對(duì)象瞬捕,包含:組件外部傳遞過(guò)來(lái),且組件內(nèi)部聲明接收了的屬性舵抹。
- context:上下文對(duì)象
- attrs: 值為對(duì)象肪虎,包含:組件外部傳遞過(guò)來(lái),但沒(méi)有在props配置中聲明的屬性, 相當(dāng)于
this.$attrs
惧蛹。 - slots: 收到的插槽內(nèi)容, 相當(dāng)于
this.$slots
扇救。 - emit: 分發(fā)自定義事件的函數(shù), 相當(dāng)于
this.$emit
。
- attrs: 值為對(duì)象肪虎,包含:組件外部傳遞過(guò)來(lái),但沒(méi)有在props配置中聲明的屬性, 相當(dāng)于
7.計(jì)算屬性與監(jiān)視
1.computed函數(shù)
與Vue2.x中computed配置功能一致
-
寫(xiě)法
import {computed} from 'vue' setup(){ ... //計(jì)算屬性——簡(jiǎn)寫(xiě) let fullName = computed(()=>{ return person.firstName + '-' + person.lastName }) //計(jì)算屬性——完整 let fullName = computed({ get(){ return person.firstName + '-' + person.lastName }, set(value){ const nameArr = value.split('-') person.firstName = nameArr[0] person.lastName = nameArr[1] } }) }
2.watch函數(shù)
與Vue2.x中watch配置功能一致
-
兩個(gè)小“坑”:
- 監(jiān)視r(shí)eactive定義的響應(yīng)式數(shù)據(jù)時(shí):oldValue無(wú)法正確獲取香嗓、強(qiáng)制開(kāi)啟了深度監(jiān)視(deep配置失效)迅腔。
- 監(jiān)視r(shí)eactive定義的響應(yīng)式數(shù)據(jù)中某個(gè)屬性時(shí):deep配置有效。
//情況一:監(jiān)視r(shí)ef定義的響應(yīng)式數(shù)據(jù) watch(sum,(newValue,oldValue)=>{ console.log('sum變化了',newValue,oldValue) },{immediate:true}) //情況二:監(jiān)視多個(gè)ref定義的響應(yīng)式數(shù)據(jù) watch([sum,msg],(newValue,oldValue)=>{ console.log('sum或msg變化了',newValue,oldValue) }) /* 情況三:監(jiān)視r(shí)eactive定義的響應(yīng)式數(shù)據(jù) 若watch監(jiān)視的是reactive定義的響應(yīng)式數(shù)據(jù)陶缺,則無(wú)法正確獲得oldValue<匦! 若watch監(jiān)視的是reactive定義的響應(yīng)式數(shù)據(jù)饱岸,則強(qiáng)制開(kāi)啟了深度監(jiān)視 */ watch(person,(newValue,oldValue)=>{ console.log('person變化了',newValue,oldValue) },{immediate:true,deep:false}) //此處的deep配置不再奏效 //情況四:監(jiān)視r(shí)eactive定義的響應(yīng)式數(shù)據(jù)中的某個(gè)屬性 watch(()=>person.job,(newValue,oldValue)=>{ console.log('person的job變化了',newValue,oldValue) },{immediate:true,deep:true}) //情況五:監(jiān)視r(shí)eactive定義的響應(yīng)式數(shù)據(jù)中的某些屬性 watch([()=>person.job,()=>person.name],(newValue,oldValue)=>{ console.log('person的job變化了',newValue,oldValue) },{immediate:true,deep:true}) //特殊情況 watch(()=>person.job,(newValue,oldValue)=>{ console.log('person的job變化了',newValue,oldValue) },{deep:true}) //此處由于監(jiān)視的是reactive素定義的對(duì)象中的某個(gè)屬性掺出,所以deep配置有效
3.watchEffect函數(shù)
watch的套路是:既要指明監(jiān)視的屬性,也要指明監(jiān)視的回調(diào)苫费。
watchEffect的套路是:不用指明監(jiān)視哪個(gè)屬性汤锨,監(jiān)視的回調(diào)中用到哪個(gè)屬性,那就監(jiān)視哪個(gè)屬性百框。
-
watchEffect有點(diǎn)像computed:
- 但computed注重的計(jì)算出來(lái)的值(回調(diào)函數(shù)的返回值)闲礼,所以必須要寫(xiě)返回值。
- 而watchEffect更注重的是過(guò)程(回調(diào)函數(shù)的函數(shù)體)铐维,所以不用寫(xiě)返回值柬泽。
//watchEffect所指定的回調(diào)中用到的數(shù)據(jù)只要發(fā)生變化,則直接重新執(zhí)行回調(diào)嫁蛇。 watchEffect(()=>{ const x1 = sum.value const x2 = person.age console.log('watchEffect配置的回調(diào)執(zhí)行了') })
8.生命周期
<div style="border:1px solid black;width:380px;float:left;margin-right:20px;"><strong>vue2.x的生命周期</strong><img src="https://cn.vuejs.org/images/lifecycle.png" alt="lifecycle_2" style="zoom:33%;width:1200px" /></div><div style="border:1px solid black;width:510px;height:985px;float:left"><strong>vue3.0的生命周期</strong><img src="https://v3.cn.vuejs.org/images/lifecycle.svg" alt="lifecycle_2" style="zoom:33%;width:2500px" /></div>
1
- Vue3.0中可以繼續(xù)使用Vue2.x中的生命周期鉤子锨并,但有有兩個(gè)被更名:
-
beforeDestroy
改名為beforeUnmount
-
destroyed
改名為unmounted
-
- Vue3.0也提供了 Composition API 形式的生命周期鉤子,與Vue2.x中鉤子對(duì)應(yīng)關(guān)系如下:
-
beforeCreate
===>setup()
-
created
=======>setup()
-
beforeMount
===>onBeforeMount
-
mounted
=======>onMounted
-
beforeUpdate
===>onBeforeUpdate
-
updated
=======>onUpdated
-
beforeUnmount
==>onBeforeUnmount
-
unmounted
=====>onUnmounted
-
9.自定義hook函數(shù)
什么是hook睬棚?—— 本質(zhì)是一個(gè)函數(shù)第煮,把setup函數(shù)中使用的Composition API進(jìn)行了封裝。
類(lèi)似于vue2.x中的mixin抑党。
自定義hook的優(yōu)勢(shì): 復(fù)用代碼, 讓setup中的邏輯更清楚易懂包警。
10.toRef
- 作用:創(chuàng)建一個(gè) ref 對(duì)象,其value值指向另一個(gè)對(duì)象中的某個(gè)屬性底靠。
- 語(yǔ)法:
const name = toRef(person,'name')
- 應(yīng)用: 要將響應(yīng)式對(duì)象中的某個(gè)屬性單獨(dú)提供給外部使用時(shí)害晦。
- 擴(kuò)展:
toRefs
與toRef
功能一致,但可以批量創(chuàng)建多個(gè) ref 對(duì)象暑中,語(yǔ)法:toRefs(person)
三篱瞎、其它 Composition API
1.shallowReactive 與 shallowRef
shallowReactive:只處理對(duì)象最外層屬性的響應(yīng)式(淺響應(yīng)式)苟呐。
shallowRef:只處理基本數(shù)據(jù)類(lèi)型的響應(yīng)式, 不進(jìn)行對(duì)象的響應(yīng)式處理痒芝。
-
什么時(shí)候使用?
- 如果有一個(gè)對(duì)象數(shù)據(jù)俐筋,結(jié)構(gòu)比較深, 但變化時(shí)只是外層屬性變化 ===> shallowReactive。
- 如果有一個(gè)對(duì)象數(shù)據(jù)严衬,后續(xù)功能不會(huì)修改該對(duì)象中的屬性澄者,而是生新的對(duì)象來(lái)替換 ===> shallowRef。
2.readonly 與 shallowReadonly
- readonly: 讓一個(gè)響應(yīng)式數(shù)據(jù)變?yōu)橹蛔x的(深只讀)请琳。
- shallowReadonly:讓一個(gè)響應(yīng)式數(shù)據(jù)變?yōu)橹蛔x的(淺只讀)粱挡。
- 應(yīng)用場(chǎng)景: 不希望數(shù)據(jù)被修改時(shí)。
3.toRaw 與 markRaw
- toRaw:
- 作用:將一個(gè)由
reactive
生成的<strong style="color:orange">響應(yīng)式對(duì)象</strong>轉(zhuǎn)為<strong style="color:orange">普通對(duì)象</strong>俄精。 - 使用場(chǎng)景:用于讀取響應(yīng)式對(duì)象對(duì)應(yīng)的普通對(duì)象询筏,對(duì)這個(gè)普通對(duì)象的所有操作,不會(huì)引起頁(yè)面更新竖慧。
- 作用:將一個(gè)由
- markRaw:
- 作用:標(biāo)記一個(gè)對(duì)象嫌套,使其永遠(yuǎn)不會(huì)再成為響應(yīng)式對(duì)象。
- 應(yīng)用場(chǎng)景:
- 有些值不應(yīng)被設(shè)置為響應(yīng)式的圾旨,例如復(fù)雜的第三方類(lèi)庫(kù)等踱讨。
- 當(dāng)渲染具有不可變數(shù)據(jù)源的大列表時(shí),跳過(guò)響應(yīng)式轉(zhuǎn)換可以提高性能砍的。
4.customRef
作用:創(chuàng)建一個(gè)自定義的 ref痹筛,并對(duì)其依賴(lài)項(xiàng)跟蹤和更新觸發(fā)進(jìn)行顯式控制。
-
實(shí)現(xiàn)防抖效果:
<template> <input type="text" v-model="keyword"> <h3>{{keyword}}</h3> </template> <script> import {ref,customRef} from 'vue' export default { name:'Demo', setup(){ // let keyword = ref('hello') //使用Vue準(zhǔn)備好的內(nèi)置ref //自定義一個(gè)myRef function myRef(value,delay){ let timer //通過(guò)customRef去實(shí)現(xiàn)自定義 return customRef((track,trigger)=>{ return{ get(){ track() //告訴Vue這個(gè)value值是需要被“追蹤”的 return value }, set(newValue){ clearTimeout(timer) timer = setTimeout(()=>{ value = newValue trigger() //告訴Vue去更新界面 },delay) } } }) } let keyword = myRef('hello',500) //使用程序員自定義的ref return { keyword } } } </script>
5.provide 與 inject
<img src="https://v3.cn.vuejs.org/images/components_provide.png" style="width:300px" />
作用:實(shí)現(xiàn)<strong style="color:#DD5145">祖與后代組件間</strong>通信
套路:父組件有一個(gè)
provide
選項(xiàng)來(lái)提供數(shù)據(jù)廓鞠,后代組件有一個(gè)inject
選項(xiàng)來(lái)開(kāi)始使用這些數(shù)據(jù)-
具體寫(xiě)法:
-
祖組件中:
setup(){ ...... let car = reactive({name:'奔馳',price:'40萬(wàn)'}) provide('car',car) ...... }
-
后代組件中:
setup(props,context){ ...... const car = inject('car') return {car} ...... }
-
6.響應(yīng)式數(shù)據(jù)的判斷
- isRef: 檢查一個(gè)值是否為一個(gè) ref 對(duì)象
- isReactive: 檢查一個(gè)對(duì)象是否是由
reactive
創(chuàng)建的響應(yīng)式代理 - isReadonly: 檢查一個(gè)對(duì)象是否是由
readonly
創(chuàng)建的只讀代理 - isProxy: 檢查一個(gè)對(duì)象是否是由
reactive
或者readonly
方法創(chuàng)建的代理
四帚稠、Composition API 的優(yōu)勢(shì)
1.Options API 存在的問(wèn)題
使用傳統(tǒng)OptionsAPI中,新增或者修改一個(gè)需求床佳,就需要分別在data滋早,methods,computed里修改 夕土。
<div style="width:600px;height:370px;overflow:hidden;float:left">
<img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f84e4e2c02424d9a99862ade0a2e4114~tplv-k3u1fbpfcp-watermark.image" style="width:600px;float:left" />
</div>
<div style="width:300px;height:370px;overflow:hidden;float:left">
<img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/e5ac7e20d1784887a826f6360768a368~tplv-k3u1fbpfcp-watermark.image" style="zoom:50%;width:560px;left" />
</div>
2.Composition API 的優(yōu)勢(shì)
我們可以更加優(yōu)雅的組織我們的代碼馆衔,函數(shù)。讓相關(guān)功能的代碼更加有序的組織在一起怨绣。
<div style="width:500px;height:340px;overflow:hidden;float:left">
<img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/bc0be8211fc54b6c941c036791ba4efe~tplv-k3u1fbpfcp-watermark.image"style="height:360px"/>
</div>
<div style="width:430px;height:340px;overflow:hidden;float:left">
<img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6cc55165c0e34069a75fe36f8712eb80~tplv-k3u1fbpfcp-watermark.image"style="height:360px"/>
</div>
五角溃、新的組件
1.Fragment
- 在Vue2中: 組件必須有一個(gè)根標(biāo)簽
- 在Vue3中: 組件可以沒(méi)有根標(biāo)簽, 內(nèi)部會(huì)將多個(gè)標(biāo)簽包含在一個(gè)Fragment虛擬元素中
- 好處: 減少標(biāo)簽層級(jí), 減小內(nèi)存占用
2.Teleport
-
什么是Teleport?——
Teleport
是一種能夠?qū)⑽覀兊?lt;strong style="color:#DD5145">組件html結(jié)構(gòu)</strong>移動(dòng)到指定位置的技術(shù)篮撑。<teleport to="移動(dòng)位置"> <div v-if="isShow" class="mask"> <div class="dialog"> <h3>我是一個(gè)彈窗</h3> <button @click="isShow = false">關(guān)閉彈窗</button> </div> </div> </teleport>
3.Suspense
等待異步組件時(shí)渲染一些額外內(nèi)容减细,讓?xiě)?yīng)用有更好的用戶(hù)體驗(yàn)
-
使用步驟:
-
異步引入組件
import {defineAsyncComponent} from 'vue' const Child = defineAsyncComponent(()=>import('./components/Child.vue'))
-
使用
Suspense
包裹組件,并配置好default
與fallback
<template> <div class="app"> <h3>我是App組件</h3> <Suspense> <template v-slot:default> <Child/> </template> <template v-slot:fallback> <h3>加載中.....</h3> </template> </Suspense> </div> </template>
-
六赢笨、其他
1.全局API的轉(zhuǎn)移
-
Vue 2.x 有許多全局 API 和配置未蝌。
-
例如:注冊(cè)全局組件驮吱、注冊(cè)全局指令等。
//注冊(cè)全局組件 Vue.component('MyButton', { data: () => ({ count: 0 }), template: '<button @click="count++">Clicked {{ count }} times.</button>' }) //注冊(cè)全局指令 Vue.directive('focus', { inserted: el => el.focus() }
-
-
Vue3.0中對(duì)這些API做出了調(diào)整:
-
將全局的API萧吠,即:
Vue.xxx
調(diào)整到應(yīng)用實(shí)例(app
)上2.x 全局 API( Vue
)3.x 實(shí)例 API ( app
)Vue.config.xxxx app.config.xxxx Vue.config.productionTip <strong style="color:#DD5145">移除</strong> Vue.component app.component Vue.directive app.directive Vue.mixin app.mixin Vue.use app.use Vue.prototype app.config.globalProperties
-
2.其他改變
data選項(xiàng)應(yīng)始終被聲明為一個(gè)函數(shù)左冬。
-
過(guò)度類(lèi)名的更改:
-
Vue2.x寫(xiě)法
.v-enter, .v-leave-to { opacity: 0; } .v-leave, .v-enter-to { opacity: 1; }
-
Vue3.x寫(xiě)法
.v-enter-from, .v-leave-to { opacity: 0; } .v-leave-from, .v-enter-to { opacity: 1; }
-
<strong style="color:#DD5145">移除</strong>keyCode作為 v-on 的修飾符,同時(shí)也不再支持
config.keyCodes
-
<strong style="color:#DD5145">移除</strong>
v-on.native
修飾符-
父組件中綁定事件
<my-component v-on:close="handleComponentEvent" v-on:click="handleNativeClickEvent" />
-
子組件中聲明自定義事件
<script> export default { emits: ['close'] } </script>
-
-
<strong style="color:#DD5145">移除</strong>過(guò)濾器(filter)
過(guò)濾器雖然這看起來(lái)很方便纸型,但它需要一個(gè)自定義語(yǔ)法拇砰,打破大括號(hào)內(nèi)表達(dá)式是 “只是 JavaScript” 的假設(shè),這不僅有學(xué)習(xí)成本狰腌,而且有實(shí)現(xiàn)成本除破!建議用方法調(diào)用或計(jì)算屬性去替換過(guò)濾器。
......