業務場景:需要在H5網站設置追蹤用戶行為(即埋點)的script代碼。
問題:無論如何修改根目錄下的index.html文件都不會生效
問題原因:在 manifest.json 文件中有個【web配置】—>【index.html模版路徑】,默認為空的,所以任你怎么改都不會生效。
解決方法:
1、在根目錄下創建一個 public 目錄,在創建一個 index.html 文件。
2、設置【index.html默認路徑】
3、在index.html填充以下代碼:
<!DOCTYPE html>
<html lang="zh-CN"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><title><%= htmlWebpackPlugin.options.title %></title><!-- Open Graph data --><!-- <meta property="og:title" content="Title Here" /> --><!-- <meta property="og:url" content="http://www.example.com/" /> --><!-- <meta property="og:image" content="http://example.com/image.jpg" /> --><!-- <meta property="og:description" content="Description Here" /> --><script>var coverSupport = 'CSS' in window && typeof CSS.supports === 'function' && (CSS.supports('top: env(a)') || CSS.supports('top: constant(a)'))document.write('<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0' + (coverSupport ? ', viewport-fit=cover' : '') + '" />')</script><link rel="stylesheet" href="<%= BASE_URL %>static/index.<%= VUE_APP_INDEX_CSS_HASH %>.css" /><!-- 埋點代碼或者公共的代碼 Start --><!-- 埋點代碼或者公共的代碼 End--></head><body><noscript><strong>Please enable JavaScript to continue.</strong></noscript><div id="app"></div><!-- built files will be auto injected --></body>
</html>
參考文檔:manifest.json 應用配置(重點)、uniapp 配置index.html 模板