第一步:
给一个错误的favicon的路径: 因为第一次加载成功后就会有缓存(再vue项目,请各位大神解惑为啥);
Microsoft Edge浏览器 相同域名首次加载成功后,会被缓存无法更改,首次加载不成功可以有效
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="icon" href="<%= BASE_URL %>xxx.ico">
<title></title>
</head>
<body>
</body>
</html>
其次再router守卫 处理
Router.beforeEach((to, from, next) => {
// 动态配置favicon
if (to.name === "no_favicon") {
updateFavicon(document.domain, "noShow.ico");
} else {
updateFavicon("name", "/favicon.ico");
}
});
updateFavicon 内容
const updateFavicon = (title, imgPath) => {
const link = document.querySelector("link[rel*='icon']");
link.rel = "icon";
link.href = process.env.BASE_URL + imgPath + "?" + new Date().getTime();
document.getElementsByTagName("head")[0].appendChild(link);
document.title = title;
};