需要合作伙伴联系我,VX(绿泡泡): w6668263      email:ye583025823@126.com

vite给HTML注入变量

安装插件:vite-plugin-html

 

npm install vite-plugin-html -D

在 vite.config.js 中配置

 

# vite.config.js

    ***

    import { defineConfig, loadEnv } from "vite";
    import { createHtmlPlugin } from "vite-plugin-html";


    const getViteEnv = (mode, target) => {
      return loadEnv(mode, process.cwd())[target];
    };

    export default ({ mode }) =>
      defineConfig({
        plugins: [
          vue(),
          createHtmlPlugin({
            inject: {
              data: {
                logo: getViteEnv(mode, "VITE_APP_LOGO"),
                title: getViteEnv(mode, "VITE_APP_TITLE"),
              },
            },
          }),
        ],

    ***

    });
inject > data 里面就是我们的数据


在 index.html 中使用变量

 <link rel="icon" href="<%= logo %>">
    <title><%= title %></title>

 




posted on 2024-03-19 13:45  龙行龘龘9527  阅读(442)  评论(0编辑  收藏  举报

导航