I think this is a very unusual use case, and I'm not talking about dynamically requiring components with webpack.
I want to be able to deploy "Apps" as Vue components on a completely different backend, so that the backend can return full .vue files over an API. The structure of the backend is irrelevant.
So I need a way in my Vue application to achieve the following:
.vue SFC file, with HTML, JS and CSS in itI found v-runtime-template which seems to be the solution for templates. But this doesn't include the JS part, does it?
Additional: I think I'm trying something like VueJS Dynamic Components, but the request URL has to be set dynamically.
Markus Oberlehner has an article that I think addresses your problem. The solution is through a externalComponent(url) function that takes the place of an import() function. I've copied Markus' function here for easy reference:
// src/utils/external-component.js
export default async function externalComponent(url) {
const name = url.split('/').reverse()[0].match(/^(.*?)\.umd/)[1];
if (window[name]) return window[name];
window[name] = new Promise((resolve, reject) => {
const script = document.createElement('script');
script.async = true;
script.addEventListener('load', () => {
resolve(window[name]);
});
script.addEventListener('error', () => {
reject(new Error(`Error loading ${url}`));
});
script.src = url;
document.head.appendChild(script);
});
return window[name];
}
According to the article, you can use the externalComponent() helper function to load external components pretty much the same way as we are used to with import().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With