Loading .vue Component from Server #46
-
Hi @huang-julien, i have been looking for a solution to load and display components during runtime for weeks. In advance, thanks for your efforts and the package 👍 Currently, i got it working with a CURRENT <template>
<div id="wrapper" class="text-center">
<component is="ExternalTestWidget" text="Hello World!" />
</div>
</template> plugins/plugin.js import { defineComponent } from "vue";
export default defineNuxtPlugin((nuxtApp) => {
// should be retrieved from API somehow.
const myComponent = {
name: "TestWidget",
template: `
<div class='text-center'>
<p>{{ text }}</p>
<v-btn @click="sendAlert()">Click</v-btn>
<v-btn @click="counter++">Counter: {{ counter }}</v-btn>
</div>
`,
data() {
return { counter: ref(1) };
},
props: {
text: { type: String, default: "undefined" },
},
methods: {
sendAlert() {
alert("Hello World!");
},
},
};
nuxtApp.vueApp.component(
"External" + `${myComponent.name}`,
defineComponent(myComponent)
);
}); GOAL i tried to AsyncComponent.vue <template>
<div>
<p>{{ text }}</p>
</div>
</template>
<script setup>
const text = "Hello World!";
</script> I would like to load this sample const AsyncComp = defineAsyncComponent({
loader: () => /* use component here */
loadingComponent: LoadingComponent,
delay: 200,
errorComponent: ErrorComponent,
timeout: 3000
}) with Object // working example const AsyncComp = defineAsyncComponent(
() =>
new Promise((resolve, reject) => {
resolve({
template: `<div @click="sendAlert()">{{ text }}</div>`,
data() {
return { text: "Hello World!" };
},
methods: {
sendAlert() {
alert("Hello! I am an alert box!!");
}
}
});
})
); For help on the solution I would be extremely grateful. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi 👋 thanks for your kind words 🙂 . |
Beta Was this translation helpful? Give feedback.
Hi 👋 thanks for your kind words 🙂 .
As stated in the playground, importing or defining a full component definition from an API is highly not recommanded since we must use
new Function
to eval the code.I don't think that importing as a module from remote sources is possible 🤔 you would need to eval the code when receiving it.
For your information there's a future feature that might suit your needs instead of this module which is server component rendering from external sources (see nuxt/nuxt#12343).
For now i would suggest you (if i understood well) to use a wrapper component with a dynamic template. What do you think ?