【问题标题】:Package nuxt app in different environment在不同的环境中打包nuxt app
【发布时间】:2019-08-27 03:05:39
【问题描述】:

我想在环境中运行npm run generate

例如:

package.json

"scripts": {
    "staging": "NODE_ENV=staging nuxt generate"
}

使用暂存环境生成dist/

npm run staging

然后在请求 API 时,我想确定我要使用的 URL 取决于我正在运行的环境

let baseURL = () => {
  switch (process.env.NODE_ENV) {
    case "it":
      return "https://example-url.com/it";
    case "staging":
      return "https://example-url.com/staging";
  }
};

const axiosClient = axios.create({
  baseURL: baseURL(),
  headers: {
    Accept: "application/json",
    "Content-Type": "application/json",
    "X-Api-Key": state().token
  }
});

baseURL() 应该返回 staging,因为我将应用程序打包在 staging env 中

【问题讨论】:

  • 那么你的代码有什么问题?请记住,环境变量是设置构建时间

标签: vue.js vuejs2 nuxt.js


【解决方案1】:

将我的应用部署到 s3 时。我想部署附加环境的代码,以便应用知道要调用哪个 URL。我设法通过在 package.json

中添加它来做到这一点
    "generate": "NUXT_ENV_STAGE=it nuxt generate",

据此https://nuxtjs.org/api/configuration-env/#automatic-injection-of-environment-variables

【讨论】:

    猜你喜欢
    • 2020-06-12
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 2018-10-05
    • 2020-01-14
    • 2021-07-15
    • 1970-01-01
    相关资源
    最近更新 更多