JavaScript/vue.js/defineAsyncComponent:修订间差异
来自康健生活
| 第1行: | 第1行: | ||
== 标题文本 == | == 标题文本 == | ||
<syntaxhighlight lang="javascript"> | |||
import { defineAsyncComponent } from 'vue' | |||
const AsyncComp = defineAsyncComponent({ | |||
loader: () => import('./foo.vue'), | |||
loadingComponent: LoadingComponent, | |||
errorComponent: ErrorComponent, | |||
delay: 200, | |||
timeout: 3000, | |||
suspensible: false, | |||
/** | |||
* | |||
* @param {*} error 错误信息对象 | |||
* @param {*} retry 一个函数,用于指示当 promise 加载器 reject 时,加载器是否应该重试 | |||
* @param {*} fail 一个函数,指示加载程序结束退出 | |||
* @param {*} attempts 允许的最大重试次数 | |||
*/ | |||
onError(error, retry, fail, attempts) { | |||
if (error.message.match(/fetch/) && attempts <= 3) { | |||
// 请求发生错误时重试,最多可尝试 3 次 | |||
retry() | |||
} else { | |||
fail() | |||
} | |||
} | |||
})</syntaxhighlight> | |||
== 异步 == | == 异步 == | ||
defineAsyncComponent 函数允许使用 '''async/await''' 处理异步问题。 | defineAsyncComponent 函数允许使用 '''async/await''' 处理异步问题。 | ||