首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在VueJS项目中的构建时使用环境变量

如何在VueJS项目中的构建时使用环境变量
EN

Stack Overflow用户
提问于 2019-02-21 02:01:25
回答 1查看 4K关注 0票数 3

我正在尝试在VueJS应用程序的CI作业的build阶段使用环境变量。我使用的是GitLab CI,其中一个可用的环境变量是CI_COMMIT_SHORT_SHA

代码语言:javascript
复制
build:
  image: node:latest
  stage: build
  variables:
    CI_COMMIT_SHORT_SHA: "$CI_COMMIT_SHORT_SHA"
  artifacts:
    paths:
      - dist/
  script:
    - npm install --progress=false
    - npm run build
    - echo "Build Complete"

下面是我在Vue中使用这个变量的方法:

代码语言:javascript
复制
<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <p>This is a static site that is served with a CloudFront distribution in front of an S3 bucket.</p>
    <p>The site is updated through a GitLab CI/CD pipeline.</p>
    <p>Commit ref: {{ commit }}</p>
    <p>Using cache invalidation</p>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  data(){
    return {
      commit: process.env.CI_COMMIT_SHORT_SHA
    }
  }
}
</script>

我看不到这个变量的作用。为了访问环境变量并将其显示在我的组件中,我还需要做些什么吗?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54792619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档