Last active
November 27, 2023 23:30
-
-
Save ffxsam/b7f415969c9529279ec35e95e5ffd7af to your computer and use it in GitHub Desktop.
Vue 3's Suspense component, ready for use in Vue 2.x (TypeScript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vue from 'vue'; | |
export const Suspense = Vue.extend({ | |
name: 'Suspense', | |
data: () => ({ | |
resolved: false, | |
}), | |
async created() { | |
const setupMethod = (this.$parent as any).setup; | |
if (!setupMethod) { | |
throw new Error( | |
'[Suspense] No setup method found - make sure Suspense is at root ' + | |
'level, just inside <template>' | |
); | |
} | |
await setupMethod(); | |
this.resolved = true; | |
}, | |
render(createElement) { | |
return createElement( | |
'div', | |
this.resolved ? this.$slots.default : this.$slots.fallback | |
); | |
}, | |
}); |
setup()
needs to be a method:
method: {
async setup() { }
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to apply this in real-life? I've added this to my main.ts. I had to add to register the component, so I had to change it to:
Then I went ahead and created an async setup()-component, and wrapped it in a -tag.
Which contains something like:
However, it remains stuck in fallback, because the setup()-function doesn't seem to exist in this.$parent. I've been going through "this", but cannot find the component/setup-function anywhere in here. I think you have a different usecase in mind perhaps (perhaps not globally?)?