W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
以下是對(duì)變化的高層次概述:
defineAsyncComponent
助手方法,用于顯式地定義異步組件component
選項(xiàng)重命名為 loader
resolve
和 reject
參數(shù),且必須返回一個(gè) Promise如需更深入的解釋,請(qǐng)繼續(xù)閱讀!
以前,異步組件是通過將組件定義為返回 Promise 的函數(shù)來創(chuàng)建的,例如:
const asyncPage = () => import('./NextPage.vue')
或者,對(duì)于帶有選項(xiàng)的更高階的組件語法:
const asyncPage = {
component: () => import('./NextPage.vue'),
delay: 200,
timeout: 3000,
error: ErrorComponent,
loading: LoadingComponent
}
現(xiàn)在,在 Vue 3 中,由于函數(shù)式組件被定義為純函數(shù),因此異步組件的定義需要通過將其包裝在新的 defineAsyncComponent
助手方法中來顯式地定義:
import { defineAsyncComponent } from 'vue'
import ErrorComponent from './components/ErrorComponent.vue'
import LoadingComponent from './components/LoadingComponent.vue'
// 不帶選項(xiàng)的異步組件
const asyncPage = defineAsyncComponent(() => import('./NextPage.vue'))
// 帶選項(xiàng)的異步組件
const asyncPageWithOptions = defineAsyncComponent({
loader: () => import('./NextPage.vue'),
delay: 200,
timeout: 3000,
errorComponent: ErrorComponent,
loadingComponent: LoadingComponent
})
對(duì) 2.x 所做的另一個(gè)更改是,component
選項(xiàng)現(xiàn)在被重命名為 loader
,以便準(zhǔn)確地傳達(dá)不能直接提供組件定義的信息。
import { defineAsyncComponent } from 'vue'
const asyncPageWithOptions = defineAsyncComponent({
loader: () => import('./NextPage.vue'),
delay: 200,
timeout: 3000,
error: ErrorComponent,
loading: LoadingComponent
})
此外,與 2.x 不同,loader 函數(shù)不再接收 resolve
和 reject
參數(shù),且必須始終返回 Promise。
// 2.x 版本
const oldAsyncComponent = (resolve, reject) => {
/* ... */
}
// 3.x 版本
const asyncComponent = defineAsyncComponent(
() =>
new Promise((resolve, reject) => {
/* ... */
})
)
有關(guān)異步組件用法的詳細(xì)信息,請(qǐng)參閱:
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: