I am dynamically loading a component from an external library file and I am receiving an error:
Uncaught TypeError: Cannot read property 'extend' of undefined
at Module.fb15 (index.ts:21)
at n (bootstrap:19)
at 0273 (bootstrap:83)
at cmp.umd.min.js:1
at universalModuleDefinition:9
at universalModuleDefinition:1
I am loading the component in this way:
//this is sfc from main app source code
<template>
<component :is="gwComponent" />
</template>
<script>
import externalComponent from "@/utils/external-component";
export default {
name: "extensions-view",
components: {},
props: ["_type_", "_instance_"],
data() {
return {
info: {
description: ""
},
instances: []
};
},
computed: {
gwComponent() {
if (this._instance_ == "all") {
return () => import("../components/ExtensionsInstances.vue");
} else {
return () =>
externalComponent(
window.location.origin +
"/assets/gw/" +
this._type_ +
"/cmp.umd.min.js"
);
}
}
},
};
</script>
// src/utils/external-component.js from main app source code
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];
}
The file cmp.umd.min.js
is created by compiling another SFC with npx vue-cli-service build --target lib --formats umd-min --no-clean --dest build/bin --name 'cmp' src/components/ExtEntryPointComponent.vue
.
I am using the code from this article to build a distributed Vue application, where users can add extensions to the main app without modifying the main app source code. However, I am receiving the error Uncaught TypeError: Cannot read property 'extend' of undefined
.