JavaScript/vue.js/defineAsyncComponent:修订间差异
来自康健生活
创建页面,内容为“== 标题文本 == == useage == === 定义异步组件 === <syntaxhighlight lang="javascript"> const AsyncComp = defineAsyncComponent(() => { // 加载函数返回Promise return new Promise((resolve, reject) => { // ...可以从服务器加载组件 resolve(→loaded component:) }) })</syntaxhighlight> === ES 模块动态导入 === <syntaxhighlight lang="javascript"> const AsyncComp = defineAsyncComponent(() => import('./components/MyComponen…” |
|||
| (未显示同一用户的17个中间版本) | |||
| 第1行: | 第1行: | ||
== 标题文本 == | == 标题文本 == | ||
[https://vuejs.org/guide/components/async Async Components] | |||
<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''' 处理异步问题。 | |||
== useage == | == useage == | ||
通常意义上只能加载 vue 后缀格式的文件。也有高分的实现 [https://github.com/FranckFreiburger/vue3-sfc-loader vue3-sfc-loader] | |||
=== 定义异步组件 === | === 定义异步组件 === | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
| 第6行: | 第40行: | ||
// 加载函数返回Promise | // 加载函数返回Promise | ||
return new Promise((resolve, reject) => { | return new Promise((resolve, reject) => { | ||
// ...可以从服务器加载组件 | fetch('http://uri/') | ||
.then(response => response.text()) | |||
.then((_) => { | |||
// ...可以从服务器加载组件 | |||
resolve(/* loaded component */) | |||
}) | |||
}) | }) | ||
})</syntaxhighlight> | })</syntaxhighlight> | ||
| 第16行: | 第54行: | ||
import('./components/MyComponent.vue') | import('./components/MyComponent.vue') | ||
)</syntaxhighlight> | )</syntaxhighlight> | ||
理论上也支持远程文件。 | |||
<syntaxhighlight lang="javascript"> | |||
defineAsyncComponent(async () => (await import('http://example.com/path/to/foo.vue')).default) | |||
</syntaxhighlight> | |||
== 打包 == | |||
=== vite === | |||
将需要的 vue 组件通过 rollup 单独打包为 es 或 umd 格式。 | |||
{{hc|head=vite.config.ts|output=<syntaxhighlight lang="bash"> | |||
// https://vitejs.dev/config/ | |||
export default defineConfig({ | |||
// ... | |||
build: { | |||
lib: {/*... */}, | |||
rollupOptions: { | |||
input: { | |||
// eq: | |||
select: path.resolve(__dirname, 'packages/select/index.vue'), | |||
// ... | |||
}, | |||
// ... | |||
}) | |||
</syntaxhighlight>}} | |||
在使用时直接引入。 | |||
<syntaxhighlight lang="javascript"> | |||
const Select = defineAsyncComponent(async () => await import(/* selecct component by es url*/)) | |||
</syntaxhighlight> | |||
==== package.json ==== | |||
<blockquote> | |||
// ... | |||
"main": "./bundle/linking-kwy.cjs.js", | |||
"module": "./bundle/linking-kwy.es.js", | |||
"exports": { | |||
".": { | |||
"import": "./bundle/linking-kwy.es.js", | |||
"require": "./bundle/linking-kwy.cjs.js" | |||
} | |||
}, | |||
// ...</blockquote> | |||
=== CSS === | |||
在打包过程中可以通过组件 vite-plugin-css-injected-by-js 将 CSS 合并入 JS 文件中,使用起来也很简单 | |||
<syntaxhighlight lang="javascript"> | |||
// https://www.npmjs.com/package/vite-plugin-css-injected-by-js | |||
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js' | |||
// ... | |||
plugins: [ | |||
cssInjectedByJsPlugin(), | |||
// ... | |||
Insert comment here | |||
</syntaxhighlight> | |||
== 注册 == | |||
=== pinia === | |||
引入的组件通过注册进而实现全局使用,这里找到一个实例。以供参考。 | |||
<syntaxhighlight lang="javascript"> | |||
import { markRaw, defineAsyncComponent } from 'vue' | |||
import { defineStore } from 'pinia' | |||
export const dynamicComponentsStoreId = 'dynamicComponents' | |||
export const useDynamicComponentsStore = defineStore(dynamicComponentsStoreId, { | |||
state: () => ({ | |||
dynamicComponents: new Map<string, any>(), | |||
}), | |||
actions: { | |||
registerComponent(type: string, component: any) { | |||
this.dynamicComponents.set(type, component) | |||
}, | |||
getComponent(type: string) { | |||
return this.dynamicComponents.get(type) | |||
}, | |||
}, | |||
}) | |||
dynamicComponentsStore.registerComponent( | |||
'CustomComponent', | |||
markRaw( | |||
defineAsyncComponent( | |||
() => import(`./components/CustomComponent/CustomComponent.vue`) | |||
) | |||
) | |||
) | |||
</syntaxhighlight> | |||
{{Unknown|https://stackoverflow.com/a/77637766}} | |||
== other == | |||
在思考远程组件加载这个方案的时候,[https://segmentfault.com/a/1190000044039990 Vue 加载远程组件的解决方案] 这篇文章给与我极大的帮助。在当今中国普遍 CTRL C 和 CRTL V 复制粘贴垃圾文的现象下,仍有愿意思考并实践的博主真的不多了。点赞。 | |||
在此方案下我的一个成功实现 | |||
<syntaxhighlight lang="javascript"> | |||
const AsyncFwdSearchCardTop = defineAsyncComponent({ | |||
loader: async () => { | |||
console.log('Loading component...') | |||
const module = await import('https://www.unpkg.com/linking-kwy@0.0.8/bundle/linking-kwy.es.js') | |||
return module.default.Linking | |||
}, | |||
// ... | |||
})</syntaxhighlight> | |||