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(海賊王)
- 耗時2年多、2600+次提交、30+個RFC、600+次PR、99位貢獻者
- 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實現(xiàn)響應(yīng)式
-
重寫虛擬DOM的實現(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 選項應(yīng)始終被聲明為一個函數(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
## 啟動
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)境中,無需打包操作,可快速的冷啟動。
- 輕量快速的熱重載(HMR)。
- 真正的按需編譯,不再等待整個應(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>
## 進入工程目錄
cd <project-name>
## 安裝依賴
npm install
## 運行
npm run dev
二、常用 Composition API
官方文檔: https://v3.cn.vuejs.org/guide/composition-api-introduction.html
1.拉開序幕的setup
- 理解:Vue3.0中一個新的配置項,值為一個函數(shù)。
- setup是所有<strong style="color:#DD5145">Composition API(組合API)</strong><i style="color:gray;font-weight:bold">“ 表演的舞臺 ”</i>。
- 組件中所用到的:數(shù)據(jù)、方法等等,均要配置在setup中。
- setup函數(shù)的兩種返回值:
- 若返回一個對象,則對象中的屬性、方法, 在模板中均可以直接使用。(重點關(guān)注?。?/li>
- <span style="color:#aad">若返回一個渲染函數(shù):則可以自定義渲染內(nèi)容。(了解)</span>
- 注意點:
- 盡量不要與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不能是一個async函數(shù),因為返回值不再是return的對象, 而是promise, 模板看不到return對象中的屬性。(后期也可以返回一個Promise實例,但需要Suspense和異步組件的配合)
- 盡量不要與Vue2.x配置混用
2.ref函數(shù)
- 作用: 定義一個響應(yīng)式的數(shù)據(jù)
- 語法:
const xxx = ref(initValue)
- 創(chuàng)建一個包含響應(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中的一個新函數(shù)——
reactive
函數(shù)。
3.reactive函數(shù)
- 作用: 定義一個<strong style="color:#DD5145">對象類型</strong>的響應(yīng)式數(shù)據(jù)(基本類型不要用它,要用
ref
函數(shù)) - 語法:
const 代理對象= reactive(源對象)
接收一個對象(或數(shù)組),返回一個<strong style="color:#DD5145">代理對象(Proxy的實例對象,簡稱proxy對象)</strong> - reactive定義的響應(yīng)式數(shù)據(jù)是“深層次的”。
- 內(nèi)部基于 ES6 的 Proxy 實現(xiàn),通過代理對象操作源對象內(nèi)部數(shù)據(jù)進行操作。
4.Vue3.0中的響應(yīng)式原理
vue2.x的響應(yīng)式
-
實現(xiàn)原理:
對象類型:通過
Object.defineProperty()
對屬性的讀取、修改進行攔截(數(shù)據(jù)劫持)。-
數(shù)組類型:通過重寫更新數(shù)組的一系列方法來實現(xiàn)攔截。(對數(shù)組的變更方法進行了包裹)。
Object.defineProperty(data, 'count', { get () {}, set () {} })
-
存在問題:
- 新增屬性、刪除屬性, 界面不會更新。
- 直接通過下標(biāo)修改數(shù)組, 界面不會自動更新。
Vue3.0的響應(yīng)式
- 實現(xiàn)原理:
- 通過Proxy(代理): 攔截對象中任意屬性的變化, 包括:屬性值的讀寫、屬性的添加、屬性的刪除等。
- 通過Reflect(反射): 對源對象的屬性進行操作。
- 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)部會自動通過
reactive
轉(zhuǎn)為<strong style="color:#DD5145">代理對象</strong>。
- 從原理角度對比:
- ref通過
Object.defineProperty()
的get
與set
來實現(xiàn)響應(yīng)式(數(shù)據(jù)劫持)。 - reactive通過使用<strong style="color:#DD5145">Proxy</strong>來實現(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ù)時模板中直接讀取<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的兩個注意點
-
setup執(zhí)行的時機
- 在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.計算屬性與監(jiān)視
1.computed函數(shù)
與Vue2.x中computed配置功能一致
-
寫法
import {computed} from 'vue' setup(){ ... //計算屬性——簡寫 let fullName = computed(()=>{ return person.firstName + '-' + person.lastName }) //計算屬性——完整 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配置功能一致
-
兩個小“坑”:
- 監(jiān)視reactive定義的響應(yīng)式數(shù)據(jù)時:oldValue無法正確獲取、強制開啟了深度監(jiān)視(deep配置失效)。
- 監(jiān)視reactive定義的響應(yīng)式數(shù)據(jù)中某個屬性時:deep配置有效。
//情況一:監(jiān)視ref定義的響應(yīng)式數(shù)據(jù) watch(sum,(newValue,oldValue)=>{ console.log('sum變化了',newValue,oldValue) },{immediate:true}) //情況二:監(jiān)視多個ref定義的響應(yīng)式數(shù)據(jù) watch([sum,msg],(newValue,oldValue)=>{ console.log('sum或msg變化了',newValue,oldValue) }) /* 情況三:監(jiān)視reactive定義的響應(yīng)式數(shù)據(jù) 若watch監(jiān)視的是reactive定義的響應(yīng)式數(shù)據(jù),則無法正確獲得oldValue?。? 若watch監(jiān)視的是reactive定義的響應(yīng)式數(shù)據(jù),則強制開啟了深度監(jiān)視 */ watch(person,(newValue,oldValue)=>{ console.log('person變化了',newValue,oldValue) },{immediate:true,deep:false}) //此處的deep配置不再奏效 //情況四:監(jiān)視reactive定義的響應(yīng)式數(shù)據(jù)中的某個屬性 watch(()=>person.job,(newValue,oldValue)=>{ console.log('person的job變化了',newValue,oldValue) },{immediate:true,deep:true}) //情況五:監(jiān)視reactive定義的響應(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素定義的對象中的某個屬性,所以deep配置有效
3.watchEffect函數(shù)
watch的套路是:既要指明監(jiān)視的屬性,也要指明監(jiān)視的回調(diào)。
watchEffect的套路是:不用指明監(jiān)視哪個屬性,監(jiān)視的回調(diào)中用到哪個屬性,那就監(jiān)視哪個屬性。
-
watchEffect有點像computed:
- 但computed注重的計算出來的值(回調(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>