简体   繁体   English

动态导入组件Vue

[英]Dynamic import component Vue

I'm trying to do this:我正在尝试这样做:

import Mainlogo from '@/logos/' +process.env.MAIN_SITE+ '.vue'

so i can get an environment based logo in my app but this doesn't work.所以我可以在我的应用程序中获得一个基于环境的标志,但这不起作用。

Syntax Error: Unexpected token, expected ;语法错误:意外令牌,预期; (96:38) (96:38)

Any ideas?有任何想法吗?

From the Vue environment variable docs :来自Vue 环境变量 docs

Note that only variables that start with VUE_APP_ will be statically embedded into the client bundle请注意,只有以 VUE_APP_ 开头的变量才会被静态嵌入到客户端包中

This means that all custom vars you want to use in your .env files must be prefixed with VUE_APP_ if you want to pull them into the Vue app.这意味着如果您想将它们拉入 Vue 应用程序,您想在.env文件中使用的所有自定义变量都必须以VUE_APP_为前缀。 For example:例如:

VUE_APP_SECRET=secret
VUE_APP_TITLE=myapp

You then access them in your code like:然后,您可以在代码中访问它们,例如:

console.log(process.env.VUE_APP_SECRET)

Ive managed to fix the issue doing the following:我设法解决了以下问题:

<component :is="mainLogoLoader" class="logo"></component>

computed: {
  mainLogoLoader () {
    return () => import('@/logos/' +process.env.MAIN_SITE+ '.vue')
  }
},

and in module.exports:并在 module.exports 中:

MAIN_SITE: '"'+process.env.site+'"'

and my build command:和我的构建命令:

#site=mysite npm run build

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM