The process.env.variable works in local development but after deploying to Heroku it stops working. I use nuxt.js

huangapple go评论56阅读模式
英文:

The process.env.variable works in local development but after deploying to Heroku it stops working. I use nuxt.js

问题

process.env.variable 在本地开发中可以工作,但部署到Heroku后再次无法工作。我使用nuxt.js

该值显示为未定义:
https://prnt.sc/l1GMK6xAQP-V

配置变量是正确的。我重新启动了应用程序,但仍然没有效果。

英文:

process.env.variable is working in local development but after deploying to Heroku its not working again. I use nuxt.js

The value shows as undefined:
https://prnt.sc/l1GMK6xAQP-V

The config var is correct. I restarted the app. still nothing.

答案1

得分: 1

.env文件:

BASE_URL=http://localhost:3000
ORIGIN_HEADER=localhost

nuxt.config.js文件:

export default {
  publicRuntimeConfig: {
    baseUrl: process.env.BASE_URL,
    originHeader: process.env.ORIGIN_HEADER
  }
}

.vue组件:

<template>
  <div>
    <p>Base URL: {{ baseUrl }}</p>
    <p>Origin Header: {{ originHeader }}</p>
  </div>
</template>
    
<script>
export default {
  computed: {
    baseUrl() {
      return this.$config.baseUrl
    },
    originHeader() {
        return this.$config.originHeader
    }
  }
}
</script>

publicRuntimeConfig 用于存放可以公开暴露的配置值,例如API端点或公共URL。

privateRuntimeConfig 则用于存放应保持私有,不应公开暴露的配置值,例如API密钥或秘密信息。

英文:

Got it!

.env file:

BASE_URL=http://localhost:3000
ORIGIN_HEADER=localhost

nuxt.config.js

export default {
  publicRuntimeConfig: {
    baseUrl: process.env.BASE_URL,
    originHeader: process.env.ORIGIN_HEADER
  }
}

.vue component:

&lt;template&gt;
  &lt;div&gt;
    &lt;p&gt;Base URL: {{ baseUrl }}&lt;/p&gt;
    &lt;p&gt;Origin Header: {{ originHeader }}&lt;/p&gt;
  &lt;/div&gt;
&lt;/template&gt;
    
&lt;script&gt;
export default {
  computed: {
    baseUrl() {
      return this.$config.baseUrl
    },
    originHeader() {
        return this.$config.originHeader
    }
  }
}
&lt;/script&gt;

publicRuntimeConfig is intended to be used for configuration values that are safe to be publicly exposed, such as API endpoints or public-facing URLs.

privateRuntimeConfig, on the other hand, is intended for configuration values that should be kept private and should not be publicly exposed, such as API keys or secrets.

huangapple
  • 本文由 发表于 2023年2月7日 04:11:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75366099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定