Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

shall we support defineAsyncComponent like vue3?or we have another way to support async component #365

Closed
JanusSpark opened this issue Feb 7, 2021 · 5 comments 路 Fixed by #380
Assignees
Labels
enhancement New feature or request

Comments

@JanusSpark
Copy link

馃啋 Your use case
want to use async component

馃啎 The solution you'd like

like defineAsyncComponent
vuejs/rfcs#148

@JanusSpark JanusSpark added the enhancement New feature or request label Feb 7, 2021
@mathe42
Copy link
Collaborator

mathe42 commented Feb 7, 2021

You can just write your own wrapper for this (this is also a vue thing and not a nuxt thing so I hope there are some packages out there that implement that for vue2)

For this

import { defineAsyncComponent } from "vue"

// with some  options
const AsyncFooWithOptions = defineAsyncComponent({
  loader: () => import("./Foo.vue"),
  loadingComponent: LoadingComponent,
  errorComponent: ErrorComponent
})

you can write something like this (not shure how events, props work out of the box but you get the idea...) - this is not tested and my contain bugs:

import { defineComponent, ref } from "vue"

function defineAsyncComponent({loader, loadingComponent, errorComponent}) {
   return defineComponent({
      setup() {
          const status = ref(0)
          const cmp = ref(null)
          const load = loader()
         
          load.then(() => status.value = 1)
          load.catch(() => status.value = -1)

          return { status, cmp }
      },
      render(h) {
        if( status.value === 0) return h(loadingComponent)
        else if( status.value === 1) return h(this.cmp.value)
        else return h(errorComponent)
      }
  })  
}

Copy link
Member

@JanusSpark Very interesting suggestion, but I agree with @mathe42 that this is a matter for upstream Vue Composition API - do raise there.

They might be open to a PR as it is not a principled decision not to have this helper: see vuejs/composition-api#390 (comment)

@mathe42
Copy link
Collaborator

mathe42 commented Feb 10, 2021

just found https://vuejs.org/v2/guide/components-dynamic-async.html

I think a complete wrapper would be

function defineAsyncComponent(options) {
   const component = options.loader()
   return {
      component,
      loading: options.loadingComponent,
      error: options.errorComponent,
      delay: options.delay,
      timeout: options.timeout
   }
}

I will create a issue (and maybe a PR) in vue/composition-api tomorow

@mathe42
Copy link
Collaborator

mathe42 commented Feb 10, 2021

Implementation PR: vuejs/composition-api#644

@mathe42
Copy link
Collaborator

mathe42 commented Feb 18, 2021

Just got merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants