Quantcast
Channel: User HMR - Stack Overflow
Viewing all articles
Browse latest Browse all 43

Optionally import Component in Vue

$
0
0

I have several menu types and want to configure the type of menu to be used in .env.local for example: VUE_APP_MENU_TYPE=2

In my javascript file I have the following:

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;

This will result in an error Failed to mount component: template or render function not defined.

I can do the following:

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;

This will work but all menus are compiled in the code, including menus that will never be used since VUE_APP_MENU_TYPE is known at compile time and will never change until you recompile.

Is it possible to import a component dynamically at compile time?


Viewing all articles
Browse latest Browse all 43

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>