Skip to content

Instantly share code, notes, and snippets.

@ffxsam
Last active November 27, 2023 23:30
Show Gist options
  • Save ffxsam/b7f415969c9529279ec35e95e5ffd7af to your computer and use it in GitHub Desktop.
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)
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
);
},
});
@ffxsam
Copy link
Author

ffxsam commented Sep 18, 2022

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