掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
經(jīng)過了漫長地迭代,Vue 3.0 終于在上 2020-09-18 發(fā)布了,帶了翻天覆地的變化,使用了 Typescript 進行了大規(guī)模的重構(gòu),帶來了 Composition API RFC 版本,類似 React Hook 一樣的寫 Vue,可以自定義自己的 hook ,讓使用者更加的靈活,接下來總結(jié)一下 vue 3.0 帶來的部分新特性。

創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、燈塔網(wǎng)站定制設(shè)計、自適應品牌網(wǎng)站建設(shè)、H5網(wǎng)站設(shè)計、成都做商城網(wǎng)站、集團公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應式網(wǎng)頁設(shè)計等建站業(yè)務,價格優(yōu)惠性價比高,為燈塔等各大城市提供網(wǎng)站開發(fā)制作服務。
setup() 函數(shù)是 vue3 中,專門為組件提供的新屬性。它為我們使用 vue3 的 Composition API 新特性提供了統(tǒng)一的入口, setup 函數(shù)會在 beforeCreate 之后、created 之前執(zhí)行, vue3 也是取消了這兩個鉤子,統(tǒng)一用 setup 代替, 該函數(shù)相當于一個生命周期函數(shù),vue 中過去的 data,methods,watch 等全部都用對應的新增 api 寫在 setup()函數(shù)中
- setup(props, context) {
- context.attrs
- context.slots
- context.parent
- context.root
- context.emit
- context.refs
- return {
- }
- }
reactive() 函數(shù)接收一個普通對象,返回一個響應式的數(shù)據(jù)對象, 想要使用創(chuàng)建的響應式數(shù)據(jù)也很簡單,創(chuàng)建出來之后,在 setup 中 return 出去,直接在 template 中調(diào)用即可
- {{name}} // test
ref() 函數(shù)用來根據(jù)給定的值創(chuàng)建一個響應式的數(shù)據(jù)對象,ref() 函數(shù)調(diào)用的返回值是一個對象,這個對象上只包含一個 value 屬性, 只在 setup 函數(shù)內(nèi)部訪問 ref 函數(shù)需要加.value
- {{count}} // 10
在 reactive 對象中訪問 ref 創(chuàng)建的響應式數(shù)據(jù)
- {{count}} -{{t}} // 10 -100
isRef() 用來判斷某個值是否為 ref() 創(chuàng)建出來的對象
toRefs() 函數(shù)可以將 reactive() 創(chuàng)建出來的響應式對象,轉(zhuǎn)換為普通的對象,只不過,這個對象上的每個屬性節(jié)點,都是 ref() 類型的響應式數(shù)據(jù)
- {{name}} // test
- {{age}} // 18
該函數(shù)用來創(chuàng)造計算屬性,和過去一樣,它返回的值是一個 ref 對象。里面可以傳方法,或者一個對象,對象中包含 set()、get()方法
6.1 創(chuàng)建只讀的計算屬性
- import { computed, defineComponent, ref } from 'vue';
- export default defineComponent({
- setup(props, context) {
- const age = ref(18)
- // 根據(jù) age 的值,創(chuàng)建一個響應式的計算屬性 readOnlyAge,它會根據(jù)依賴的 ref 自動計算并返回一個新的 ref
- const readOnlyAge = computed(() => age.value++) // 19
- return {
- age,
- readOnlyAge
- }
- }
- });
6.2 通過 set()、get()方法創(chuàng)建一個可讀可寫的計算屬性
watch 函數(shù)用來偵聽特定的數(shù)據(jù)源,并在回調(diào)函數(shù)中執(zhí)行副作用。默認情況是懶執(zhí)行的,也就是說僅在偵聽的源數(shù)據(jù)變更時才執(zhí)行回調(diào)。
7.1 監(jiān)聽用 reactive 聲明的數(shù)據(jù)源
7.2 監(jiān)聽用 ref 聲明的數(shù)據(jù)源
7.3 同時監(jiān)聽多個值
7.4 stop 停止監(jiān)聽
在 setup() 函數(shù)內(nèi)創(chuàng)建的 watch 監(jiān)視,會在當前組件被銷毀的時候自動停止。如果想要明確地停止某個監(jiān)視,可以調(diào)用 watch() 函數(shù)的返回值即可,語法如下:
新版的生命周期函數(shù),可以按需導入到組件中,且只能在 setup() 函數(shù)中使用, 但是也可以在 setup 自定義, 在 setup 中使用
通過 refs 來回去真實 dom 元素, 這個和 react 的用法一樣,為了獲得對模板內(nèi)元素或組件實例的引用,我們可以像往常一樣在 setup()中聲明一個 ref 并返回它
- 1111
通過 vue 實例上 config 的配置,包含 Vue 應用程序全局配置的對象。您可以在掛載應用程序之前修改下面列出的屬性:
- const app = Vue.createApp({})
- app.config = {...}
為組件渲染功能和觀察程序期間的未捕獲錯誤分配處理程序。錯誤和應用程序?qū)嵗龑⒄{(diào)用處理程序
- app.config.errorHandler = (err, vm, info) => {}
可以在應用程序內(nèi)的任何組件實例中訪問的全局屬性,組件的屬性將具有優(yōu)先權(quán)。這可以代替 Vue 2.xVue.prototype 擴展:
- const app = Vue.createApp({})
- app.config.globalProperties.$http = 'xxxxxxxxs'
可以在組件用通過 getCurrentInstance() 來獲取全局 globalProperties 中配置的信息,getCurrentInstance 方法獲取當前組件的實例,然后通過 ctx 屬性獲得當前上下文,這樣我們就能在 setup 中使用 router 和 vuex, 通過這個屬性我們就可以操作變量、全局屬性、組件屬性等等
- setup( ) {
- const { ctx } = getCurrentInstance();
- ctx.$http
- }
在開始介紹 Vue 的 Suspense 組件之前,我們有必要先了解一下 React 的 Suspense 組件,因為他們的功能類似。
React.lazy 接受一個函數(shù),這個函數(shù)需要動態(tài)調(diào)用 import()。它必須返回一個 Promise,該 Promise 需要 resolve 一個 default export 的 React 組件。
- import React, { Suspense } from 'react';
- const myComponent = React.lazy(() => import('./Component'));
- function MyComponent() {
- return (
}>Loading...
Vue3 也新增了 React.lazy 類似功能的 defineAsyncComponent 函數(shù),處理動態(tài)引入(的組件)。defineAsyncComponent 可以接受返回承諾的工廠函數(shù)。當您從服務器檢索到組件定義時,應該調(diào)用 Promise 的解析回調(diào)。您還可以調(diào)用 reject(reason)來指示負載已經(jīng)失敗
- import { defineAsyncComponent } from 'vue'
- const AsyncComp = defineAsyncComponent(() =>
- import('./components/AsyncComponent.vue')
- )
- app.component('async-component', AsyncComp)
Vue3 也新增了 Suspense 組件:
- Loading ...
一個完成的 vue 3.x 完整組件模版結(jié)構(gòu)包含了:組件名稱、 props、components、setup(hooks、computed、watch、methods 等)
- {{name}}
- {{count}}
- {{item.name}}
- {{name}}
- {{count}}
- {{item.name}}
UI 組件庫

我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流