作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何从 Vue 应用程序外部化和使用环境变量:
.env
的经典用法
VUE_APP_
的文件变体prefixed 对这个问题没有帮助,因为它们的值在构建阶段被注入(inject)到代码中:一旦构建它们就不是变量。
dotenv
和一些额外的配置;但我无法将它与
this VueCLI 3 official guide 中的配置放在一起.该解决方案虽然不需要采用类似的方法,但我只是想找到一条出路。
最佳答案
我想我已经完成了克服这个案子。我把决议留在这里。
.env.development
中定义特定于环境的环境变量(用于开发目的)并将它们也添加到 Pod configuration具有相应的值。 configuration.js
在你的 Vue 项目源文件夹中的某处创建文件。它将充当一个包装器,用于确定运行时是开发(本地)还是生产(容器)。就像显示的 here , 但导入/配置 dotenv
不需要:export default class Configuration {
static get EnvConfig () {
return {
envKey1: '$ENV_KEY_1',
envKey2: '$ENV_KEY_2'
}
}
static value (key) {
// If the key does not exist in the EnvConfig object of the class, return null
if (!this.EnvConfig.hasOwnProperty(key)) {
console.error(`Configuration: There is no key named "${key}". Please add it in Configuration class.`)
return
}
// Get the value
const value = this.EnvConfig[key]
// If the value is null, return
if (!value) {
console.error(`Configuration: Value for "${key}" is not defined`)
return
}
if (!value.startsWith('$VUE_APP_')) {
// value was already replaced, it seems we are in production (containerized).
return value
}
// value was not replaced, it seems we are in development.
const envName = value.substr(1) // Remove $ and get current value from process.env
const envValue = process.env[envName]
if (!envValue) {
console.error(`Configuration: Environment variable "${envName}" is not defined`)
return
}
return envValue
}
}
entrypoint.sh
.经过一些修改,它看起来如下所示:#!/bin/bash
function join_by { local IFS="$1"; shift; echo "$*"; }
# Find vue env vars
vars=$(env | grep VUE_APP_ | awk -F = '{print "$"$1}')
vars=$(join_by ',' $vars)
echo "Found variables $vars"
for file in /app/js/app.*;
do
echo "Processing $file ...";
# Use the existing JS file as template
cp $file $file.tmpl
envsubst "$vars" < $file.tmpl > $file
rm $file.tmpl
done
nginx -g 'daemon off;'
Dockerfile
, 添加 CMD
用于运行 entrypoint.sh
上面的脚本 作为容器创建期间的引导脚本 .这样,每次启动容器时,它都会从 pod 配置中获取环境变量,并将其注入(inject)到步骤 2 中所示的 Configuration 类中。# build stage
FROM node:lts-alpine as build-stage
# make the 'app' folder the current working directory
WORKDIR /app
# Copy package*.json and install dependencies in a separaate step to enable caching
COPY package*.json ./
RUN npm install
# copy project files and folders to the current working directory
COPY ./ .
# install dependencies and build app for production with minification
RUN npm run build
# Production stage
FROM nginx as production-stage
RUN mkdir /app
# copy 'dist' content from the previous stage i.e. build
COPY --from=build-stage /app/dist /app
# copy nginx configuration
COPY nginx.conf /etc/nginx/nginx.conf
# Copy the bootstrapping script to inject environment-specific values and pass it as argument current to entrypoint
COPY entrypoint.sh entrypoint.sh
# Make the file executable
RUN chmod +x ./entrypoint.sh
CMD ["./entrypoint.sh"]
process.env
使用我们的包装器配置类,如
Configuration.value('envKey1')
.瞧!
关于docker - VueCLI3 应用程序(nginx/docker)使用环境特定变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57807248/
我正在尝试将我的 VueJS 项目从 VueCLI 2 迁移到 3。所有文件都复制到 src 文件夹中。当我尝试使用 npm runserve 在浏览器中查看它时,我得到这个 HTML,没有 目前:
我是一名优秀的程序员,十分优秀!