vue項目index.html中使用環(huán)境變量的代碼示例
1,Vue-CLI(Webpack)
<!DOCTYPE html>
<html lang="">
<head>
<link rel="icon" href="<%= BASE_URL %>favicon.ico" rel="external nofollow" />
<title><%= htmlWebpackPlugin.options.title %></title>
</head>
<body>
<div id="app"></div>
<script>
console.log('<%= NODE_ENV %>')
console.log('<%= BASE_URL %>')
console.log('<%= VUE_APP_CONTEXT %>')
</script>
</body>
</html>
在 vue-cli 創(chuàng)建的項目中,
index.html使用環(huán)境變量時通過<%= xxx %>。參考除了
VUE_APP_*變量之外,始終可使用的變量有2個NODE_ENV和BASE_URL。參考vue-cli 內(nèi)置了
htmlWebpackPlugin插件,其中htmlWebpackPlugin.options.title默認取的是package.json中的name字段。
2,Vite
<!DOCTYPE html>
<html lang="en">
<head>
<title>%MODE%</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
<script>
console.log('%MODE%')
</script>
<script type="module">
console.log(import.meta.env.MODE)
</script>
</body>
</html>
編譯結(jié)果
<script>console.log('development')</script>
<script type="module" src="/index.html?html-proxy&index=0.js"></script>
- vite 創(chuàng)建的項目中,
index.html使用環(huán)境變量有2種方式:
- 通過
%xxx%(參考) - 在
<script type="module">中通過 es6 的模塊語法,使用 Vite 創(chuàng)建的import.meta.env對象上暴露的環(huán)境變量。
- 始終可使用的變量有4個。參考
以上。
總結(jié)
到此這篇關(guān)于vue項目index.html中使用環(huán)境變量的文章就介紹到這了,更多相關(guān)vue index.html使用環(huán)境變量內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue監(jiān)視器@Watch創(chuàng)建執(zhí)行immediate方式
這篇文章主要介紹了vue監(jiān)視器@Watch創(chuàng)建執(zhí)行immediate方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
VUE3+vite項目中動態(tài)引入組件與異步組件的詳細實例
在做vue3項目中時,每次使用都需要先進行引入,下面這篇文章主要給大家介紹了關(guān)于VUE3+vite項目中動態(tài)引入組件與異步組件的相關(guān)資料,需要的朋友可以參考下2022-09-09
Vue實現(xiàn)contenteditable元素雙向綁定的方法詳解
contenteditable是所有HTML元素都有的枚舉屬性,表示元素是否可以被用戶編輯。本文將詳細介紹如何實現(xiàn)contenteditable元素的雙向綁定,需要的可以參考一下2022-05-05
Ant?Design?Vue中的table與pagination的聯(lián)合使用方式
這篇文章主要介紹了Ant?Design?Vue中的table與pagination的聯(lián)合使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10

