Vue3快速上手
<img src="https://user-images.githubusercontent.com/499550/93624428-53932780-f9ae-11ea-8d16-af949e16a09f.png" style="width:200px" />
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)式
-
重寫虛擬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ì)如下:
- 開發(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>
## 安裝依賴
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è)對(duì)象,則對(duì)象中的屬性、方法, 在模板中均可以直接使用。(重點(diǎn)關(guān)注!)
- <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)稱ref對(duì)象)</strong>。
- JS中操作數(shù)據(jù):
xxx.value
- 模板中讀取數(shù)據(jù): 不需要.value,直接:
<div>{{xxx}}</div>
- 備注:
- 接收的數(shù)據(jù)可以是:基本類型、也可以是對(duì)象類型。
- 基本類型的數(shù)據(jù):響應(yīng)式依然是靠
Object.defineProperty()
的get
與set
完成的。 - 對(duì)象類型的數(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ì)象類型</strong>的響應(yīng)式數(shù)據(jù)(基本類型不要用它,要用
ref
函數(shù)) - 語(yǔ)法:
const 代理對(duì)象= reactive(源對(duì)象)
接收一個(gè)對(duì)象(或數(shù)組),返回一個(gè)<strong style="color:#DD5145">代理對(duì)象(Proxy的實(shí)例對(duì)象,簡(jiǎn)稱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ì)象類型:通過(guò)
Object.defineProperty()
對(duì)屬性的讀取、修改進(jìn)行攔截(數(shù)據(jù)劫持)。-
數(shù)組類型:通過(guò)重寫更新數(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ì)象中任意屬性的變化, 包括:屬性值的讀寫、屬性的添加、屬性的刪除等。
- 通過(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">基本類型數(shù)據(jù)</strong>。
- reactive用來(lái)定義:<strong style="color:#DD5145">對(duì)象(或數(shù)組)類型數(shù)據(jù)</strong>。
- 備注:ref也可以用來(lái)定義<strong style="color:#DD5145">對(duì)象(或數(shù)組)類型數(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),但沒有在props配置中聲明的屬性, 相當(dāng)于
this.$attrs
。 - slots: 收到的插槽內(nèi)容, 相當(dāng)于
this.$slots
。 - emit: 分發(fā)自定義事件的函數(shù), 相當(dāng)于
this.$emit
。
- attrs: 值為對(duì)象,包含:組件外部傳遞過(guò)來(lái),但沒有在props配置中聲明的屬性, 相當(dāng)于
7.計(jì)算屬性與監(jiān)視
1.computed函數(shù)
與Vue2.x中computed配置功能一致
-
寫法
import {computed} from 'vue' setup(){ ... //計(jì)算屬性——簡(jiǎn)寫 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)制開啟了深度監(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)制開啟了深度監(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ù)的返回值),所以必須要寫返回值。
- 而watchEffect更注重的是過(guò)程(回調(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>