美文网首页
vue-cli在index.html判断环境变量加载不同代码

vue-cli在index.html判断环境变量加载不同代码

作者: GuitarHusky | 来源:发表于2021-12-21 15:52 被阅读0次

在vue-cli生成的项目中,index.html下是无法直接获取到环境变量的,但是可以使用webpack提供的特殊语法获取的服务中的变量 <% %>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width,initial-scale=1.0" />
        <meta name="keywords" content="" />
        <meta name="description" content="" />
        <link rel="icon" href="<%= BASE_URL %>favicon.ico" />
        <title><%= htmlWebpackPlugin.options.title %></title>

        <% if (process.env.NODE_ENV === 'production' ) { %>
        <script>
            code.....
        </script>
        <% } else {%>
        <script>
            code.....
        </script>
        <% } %>
    </head>
    <body>
        <noscript>
            <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
        </noscript>
        <div id="app"></div>
        <!-- built files will be auto injected -->
    </body>
</html>

相关文章

网友评论

      本文标题:vue-cli在index.html判断环境变量加载不同代码

      本文链接:https://www.haomeiwen.com/subject/ktuxqrtx.html