解决 uniapp 修改index.html文件不生效的问题

业务场景:需要在H5网站设置追踪用户行为(即埋点)的script代码。

问题:无论如何修改根目录下的index.html文件都不会生效

问题原因:在 manifest.json 文件中有个【web配置】--->【index.html模版路径】,默认为空的,所以任你怎么改都不会生效。

解决方法:

1、在根目录下创建一个 public 目录,在创建一个 index.html 文件。

2、设置【index.html默认路径】

3、在index.html填充以下代码:

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 模板

相关推荐
编程之路从0到17 分钟前
JSI入门指南
前端·c++·react native
开始学java8 分钟前
别再写“一锅端”的 useEffect!聊聊 React 副作用的逻辑分离
前端
百度地图汽车版13 分钟前
【智图译站】基于异步时空图卷积网络的不规则交通预测
前端·后端
qq_124987075317 分钟前
基于Spring Boot的“味蕾探索”线上零食购物平台的设计与实现(源码+论文+部署+安装)
java·前端·数据库·spring boot·后端·小程序
编程之路从0到118 分钟前
React Native 之Android端 Bolts库
android·前端·react native
小酒星小杜18 分钟前
在AI时代,技术人应该每天都要花两小时来构建一个自身的构建系统 - Build 篇
前端·vue.js·架构
奔跑的web.21 分钟前
TypeScript 全面详解:对象类型的语法规则
开发语言·前端·javascript·typescript·vue
江上月51325 分钟前
JMeter中级指南:从数据提取到断言校验全流程掌握
java·前端·数据库
代码猎人27 分钟前
forEach和map方法有哪些区别
前端
恋猫de小郭28 分钟前
Google DeepMind :RAG 已死,无限上下文是伪命题?RLM 如何用“代码思维”终结 AI 的记忆焦虑
前端·flutter·ai编程