JavaScript/vue.js/defineAsyncComponent:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
第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}}