I have multiple menu types and would like to determine which type of menu to use by configuring it in .env.local. For example: VUE_APP_MENU_TYPE=2
Here is the code snippet from my javascript file:
let menu = false;
if (process.env.VUE_APP_MENU_TYPE === "2") {
menu = require("./type2/Type2.vue");
}
if (menu === false) {//default menu if env is missing
menu = require("./default/Default.vue");
}
export default menu;
However, this may result in an error message saying
Failed to mount component: template or render function not defined.
An alternative approach would be:
import Default from "./default/Default.vue";
import Type2 from "./type2/Type2.vue";
let menu = Default;
if (process.env.VUE_APP_MENU_TYPE === "2") {
menu = Type2;
}
export default menu;
Although this solution works, all menu components are compiled into the code, even those that will never be used because VUE_APP_MENU_TYPE is known at compile time and does not change until recompilation.
Is there a way to dynamically import a component at compile time?