Vue3快速上手
<img src="https://user-images.githubusercontent.com/499550/93624428-53932780-f9ae-11ea-8d16-af949e16a09f.png" style="width:200px" />
1.Vue3簡介
- 2020年9月18日隔心,Vue.js發(fā)布3.0版本,代號: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帶來了什么
1.性能的提升
打包大小減少41%
初次渲染快55%, 更新渲染快133%
-
內(nèi)存減少54%
......
2.源碼的升級
使用Proxy代替defineProperty實(shí)現(xiàn)響應(yīng)式
-
重寫虛擬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
## 安裝或者升級你的@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)勢如下:
- 開發(fā)環(huán)境中,無需打包操作祥得,可快速的冷啟動(dòng)兔沃。
- 輕量快速的熱重載(HMR)。
- 真正的按需編譯级及,不再等待整個(gè)應(yīng)用編譯完成乒疏。
- 傳統(tǒng)構(gòu)建 與 vite構(gòu)建對比圖
<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>
## 安裝依賴
npm install
## 運(yùn)行
npm run dev
二、常用 Composition API
官方文檔: https://v3.cn.vuejs.org/guide/composition-api-introduction.html
1.拉開序幕的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è)對象殿雪,則對象中的屬性、方法, 在模板中均可以直接使用锋爪。(重點(diǎn)關(guān)注1铩)
- <span style="color:#aad">若返回一個(gè)渲染函數(shù):則可以自定義渲染內(nèi)容爸业。(了解)</span>
- 注意點(diǎn):
- 盡量不要與Vue2.x配置混用
- Vue2.x配置(data、methos亏镰、computed...)中<strong style="color:#DD5145">可以訪問到</strong>setup中的屬性扯旷、方法。
- 但在setup中<strong style="color:#DD5145">不能訪問到</strong>Vue2.x配置(data索抓、methos钧忽、computed...)。
- 如果有重名, setup優(yōu)先逼肯。
- setup不能是一個(gè)async函數(shù)耸黑,因?yàn)榉祷刂挡辉偈莚eturn的對象, 而是promise, 模板看不到return對象中的屬性。(后期也可以返回一個(gè)Promise實(shí)例篮幢,但需要Suspense和異步組件的配合)
- 盡量不要與Vue2.x配置混用
2.ref函數(shù)
- 作用: 定義一個(gè)響應(yīng)式的數(shù)據(jù)
- 語法:
const xxx = ref(initValue)
- 創(chuàng)建一個(gè)包含響應(yīng)式數(shù)據(jù)的<strong style="color:#DD5145">引用對象(reference對象大刊,簡稱ref對象)</strong>。
- JS中操作數(shù)據(jù):
xxx.value
- 模板中讀取數(shù)據(jù): 不需要.value三椿,直接:
<div>{{xxx}}</div>
- 備注:
- 接收的數(shù)據(jù)可以是:基本類型缺菌、也可以是對象類型。
- 基本類型的數(shù)據(jù):響應(yīng)式依然是靠
Object.defineProperty()
的get
與set
完成的搜锰。 - 對象類型的數(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">對象類型</strong>的響應(yīng)式數(shù)據(jù)(基本類型不要用它,要用
ref
函數(shù)) - 語法:
const 代理對象= reactive(源對象)
接收一個(gè)對象(或數(shù)組)纽乱,返回一個(gè)<strong style="color:#DD5145">代理對象(Proxy的實(shí)例對象蛾绎,簡稱proxy對象)</strong> - reactive定義的響應(yīng)式數(shù)據(jù)是“深層次的”。
- 內(nèi)部基于 ES6 的 Proxy 實(shí)現(xiàn)鸦列,通過代理對象操作源對象內(nèi)部數(shù)據(jù)進(jìn)行操作租冠。
4.Vue3.0中的響應(yīng)式原理
vue2.x的響應(yīng)式
-
實(shí)現(xiàn)原理:
對象類型:通過
Object.defineProperty()
對屬性的讀取、修改進(jìn)行攔截(數(shù)據(jù)劫持)薯嗤。-
數(shù)組類型:通過重寫更新數(shù)組的一系列方法來實(shí)現(xiàn)攔截顽爹。(對數(shù)組的變更方法進(jìn)行了包裹)。
Object.defineProperty(data, 'count', { get () {}, set () {} })
-
存在問題:
- 新增屬性骆姐、刪除屬性, 界面不會(huì)更新镜粤。
- 直接通過下標(biāo)修改數(shù)組, 界面不會(huì)自動(dòng)更新。
Vue3.0的響應(yīng)式
- 實(shí)現(xiàn)原理:
- 通過Proxy(代理): 攔截對象中任意屬性的變化, 包括:屬性值的讀寫玻褪、屬性的添加肉渴、屬性的刪除等。
- 通過Reflect(反射): 對源對象的屬性進(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對比ref
- 從定義數(shù)據(jù)角度對比:
- ref用來定義:<strong style="color:#DD5145">基本類型數(shù)據(jù)</strong>同规。
- reactive用來定義:<strong style="color:#DD5145">對象(或數(shù)組)類型數(shù)據(jù)</strong>。
- 備注:ref也可以用來定義<strong style="color:#DD5145">對象(或數(shù)組)類型數(shù)據(jù)</strong>, 它內(nèi)部會(huì)自動(dòng)通過
reactive
轉(zhuǎn)為<strong style="color:#DD5145">代理對象</strong>。
- 從原理角度對比:
- ref通過
Object.defineProperty()
的get
與set
來實(shí)現(xiàn)響應(yīng)式(數(shù)據(jù)劫持)券勺。 - reactive通過使用<strong style="color:#DD5145">Proxy</strong>來實(shí)現(xiàn)響應(yīng)式(數(shù)據(jù)劫持), 并通過<strong style="color:#DD5145">Reflect</strong>操作<strong style="color:orange">源對象</strong>內(nèi)部的數(shù)據(jù)绪钥。
- ref通過
- 從使用角度對比:
- 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:值為對象跪楞,包含:組件外部傳遞過來,且組件內(nèi)部聲明接收了的屬性侣灶。
- context:上下文對象
- attrs: 值為對象甸祭,包含:組件外部傳遞過來,但沒有在props配置中聲明的屬性, 相當(dāng)于
this.$attrs
褥影。 - slots: 收到的插槽內(nèi)容, 相當(dāng)于
this.$slots
池户。 - emit: 分發(fā)自定義事件的函數(shù), 相當(dāng)于
this.$emit
。
- attrs: 值為對象甸祭,包含:組件外部傳遞過來,但沒有在props配置中聲明的屬性, 相當(dāng)于
7.計(jì)算屬性與監(jiān)視
1.computed函數(shù)
與Vue2.x中computed配置功能一致
-
寫法
import {computed} from 'vue' setup(){ ... //計(jì)算屬性——簡寫 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無法正確獲取凡怎、強(qiáng)制開啟了深度監(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ù)统倒,則無法正確獲得oldValueU洹! 若watch監(jiān)視的是reactive定義的響應(yīng)式數(shù)據(jù)房匆,則強(qiáng)制開啟了深度監(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素定義的對象中的某個(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ì)算出來的值(回調(diào)函數(shù)的返回值)花竞,所以必須要寫返回值。
- 而watchEffect更注重的是過程(回調(diào)函數(shù)的函數(shù)體)掸哑,所以不用寫返回值约急。
//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中鉤子對應(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)行了封裝服猪。
類似于vue2.x中的mixin供填。
自定義hook的優(yōu)勢: 復(fù)用代碼, 讓setup中的邏輯更清楚易懂拐云。
10.toRef
- 作用:創(chuàng)建一個(gè) ref 對象,其value值指向另一個(gè)對象中的某個(gè)屬性近她。
- 語法:
const name = toRef(person,'name')
- 應(yīng)用: 要將響應(yīng)式對象中的某個(gè)屬性單獨(dú)提供給外部使用時(shí)叉瘩。
- 擴(kuò)展:
toRefs
與toRef
功能一致,但可以批量創(chuàng)建多個(gè) ref 對象粘捎,語法:toRefs(person)
三薇缅、其它 Composition API
1.shallowReactive 與 shallowRef
shallowReactive:只處理對象最外層屬性的響應(yīng)式(淺響應(yīng)式)。
shallowRef:只處理基本數(shù)據(jù)類型的響應(yīng)式, 不進(jìn)行對象的響應(yīng)式處理攒磨。
-
什么時(shí)候使用?
- 如果有一個(gè)對象數(shù)據(jù)泳桦,結(jié)構(gòu)比較深, 但變化時(shí)只是外層屬性變化 ===> shallowReactive。
- 如果有一個(gè)對象數(shù)據(jù)娩缰,后續(xù)功能不會(huì)修改該對象中的屬性灸撰,而是生新的對象來替換 ===> shallowRef。
2.readonly 與 shallowReadonly
- readonly: 讓一個(gè)響應(yīng)式數(shù)據(jù)變?yōu)橹蛔x的(深只讀)拼坎。
- shallowReadonly:讓一個(gè)響應(yīng)式數(shù)據(jù)變?yōu)橹蛔x的(淺只讀)浮毯。
- 應(yīng)用場景: 不希望數(shù)據(jù)被修改時(shí)。
3.toRaw 與 markRaw
- toRaw:
- 作用:將一個(gè)由
reactive
生成的<strong style="color:orange">響應(yīng)式對象</strong>轉(zhuǎn)為<strong style="color:orange">普通對象</strong>泰鸡。 - 使用場景:用于讀取響應(yīng)式對象對應(yīng)的普通對象债蓝,對這個(gè)普通對象的所有操作,不會(huì)引起頁面更新盛龄。
- 作用:將一個(gè)由
- markRaw:
- 作用:標(biāo)記一個(gè)對象饰迹,使其永遠(yuǎn)不會(huì)再成為響應(yīng)式對象。
- 應(yīng)用場景:
- 有些值不應(yīng)被設(shè)置為響應(yīng)式的余舶,例如復(fù)雜的第三方類庫等蹦锋。
- 當(dāng)渲染具有不可變數(shù)據(jù)源的大列表時(shí),跳過響應(yīng)式轉(zhuǎn)換可以提高性能欧芽。
4.customRef
作用:創(chuàng)建一個(gè)自定義的 ref莉掂,并對其依賴項(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 //通過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)來提供數(shù)據(jù)千扔,后代組件有一個(gè)inject
選項(xiàng)來開始使用這些數(shù)據(jù)-
具體寫法:
-
祖組件中:
setup(){ ...... let car = reactive({name:'奔馳',price:'40萬'}) provide('car',car) ...... }
-
后代組件中:
setup(props,context){ ...... const car = inject('car') return {car} ...... }
-
6.響應(yīng)式數(shù)據(jù)的判斷
- isRef: 檢查一個(gè)值是否為一個(gè) ref 對象
- isReactive: 檢查一個(gè)對象是否是由
reactive
創(chuàng)建的響應(yīng)式代理 - isReadonly: 檢查一個(gè)對象是否是由
readonly
創(chuàng)建的只讀代理 - isProxy: 檢查一個(gè)對象是否是由
reactive
或者readonly
方法創(chuàng)建的代理
四憎妙、Composition API 的優(yōu)勢
1.Options API 存在的問題
使用傳統(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)勢
我們可以更加優(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中: 組件可以沒有根標(biāo)簽, 內(nèi)部會(huì)將多個(gè)標(biāo)簽包含在一個(gè)Fragment虛擬元素中
- 好處: 減少標(biāo)簽層級, 減小內(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)容罕伯,讓應(yīng)用有更好的用戶體驗(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 和配置追他。
-
例如:注冊全局組件坟募、注冊全局指令等。
//注冊全局組件 Vue.component('MyButton', { data: () => ({ count: 0 }), template: '<button @click="count++">Clicked {{ count }} times.</button>' }) //注冊全局指令 Vue.directive('focus', { inserted: el => el.focus() }
-
-
Vue3.0中對這些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ù)懈糯。
-
過度類名的更改:
-
Vue2.x寫法
.v-enter, .v-leave-to { opacity: 0; } .v-leave, .v-enter-to { opacity: 1; }
-
Vue3.x寫法
.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>過濾器(filter)
過濾器雖然這看起來很方便单雾,但它需要一個(gè)自定義語法昂利,打破大括號內(nèi)表達(dá)式是 “只是 JavaScript” 的假設(shè),這不僅有學(xué)習(xí)成本铁坎,而且有實(shí)現(xiàn)成本蜂奸!建議用方法調(diào)用或計(jì)算屬性去替換過濾器。
......