JavaScript/vue.js/defineAsyncComponent:修订间差异
来自康健生活
< JavaScript | vue.js
| 第56行: | 第56行: | ||
const Select = defineAsyncComponent(async () => await import(/* selecct component by es url*/)) | const Select = defineAsyncComponent(async () => await import(/* selecct component by es url*/)) | ||
</syntaxhighlight> | </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}} | |||
2025年4月14日 (一) 11:37的版本
标题文本
异步
defineAsyncComponent 函数允许使用 async/await 处理异步问题。
useage
通常意义上只能加载 vue 后缀格式的文件。也有高分的实现 vue3-sfc-loader
定义异步组件
const AsyncComp = defineAsyncComponent(() => {
// 加载函数返回Promise
return new Promise((resolve, reject) => {
fetch('http://uri/')
.then(response => response.text())
.then((_) => {
// ...可以从服务器加载组件
resolve(/* loaded component */)
})
})
})
ES 模块动态导入
const AsyncComp = defineAsyncComponent(() =>
import('./components/MyComponent.vue')
)
理论上也支持远程文件。
defineAsyncComponent(async () => (await import('http://example.com/path/to/foo.vue')).default)
打包
vite
将需要的 vue 组件通过 rollup 单独打包为 es 或 umd 格式。
vite.config.ts
// https://vitejs.dev/config/
export default defineConfig({
// ...
build: {
lib: {/*... */},
rollupOptions: {
input: {
// eq:
select: path.resolve(__dirname, 'packages/select/index.vue'),
// ...
},
// ...
})
在使用时直接引入。
const Select = defineAsyncComponent(async () => await import(/* selecct component by es url*/))
注册
pinia
引入的组件通过注册进而实现全局使用,这里找到一个实例。以供参考。
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`)
)
)
)
(来源摘自互联网示例,作者 https://stackoverflow.com/a/77637766)
No categories