欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue項(xiàng)目index.html中使用環(huán)境變量的代碼示例

 更新時(shí)間:2024年02月26日 08:25:51   作者:下雪天的夏風(fēng)  
在Vue3中使用環(huán)境變量的方式與Vue2基本相同,下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目index.html中使用環(huán)境變量的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下

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)建的項(xiàng)目中,index.html 使用環(huán)境變量時(shí)通過 <%= xxx %>。參考

  • 除了 VUE_APP_* 變量之外,始終可使用的變量有2個(gè) NODE_ENV 和 BASE_URL參考

  • vue-cli 內(nèi)置了 htmlWebpackPlugin 插件,其中 htmlWebpackPlugin.options.title 默認(rèn)取的是 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&amp;index=0.js"></script>
  • vite 創(chuàng)建的項(xiàng)目中,index.html 使用環(huán)境變量有2種方式:
  • 通過 %xxx%參考
  • 在 <script type="module"> 中通過 es6 的模塊語法,使用 Vite 創(chuàng)建的 import.meta.env 對(duì)象上暴露的環(huán)境變量。
  • 始終可使用的變量有4個(gè)。參考

以上。

總結(jié)

到此這篇關(guān)于vue項(xiàng)目index.html中使用環(huán)境變量的文章就介紹到這了,更多相關(guān)vue index.html使用環(huán)境變量?jī)?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論