我们公司正在将我们的一个前端项目迁移到带有 Webpack 5 的 Angular 11。我们目前使用的是 Angular 10,支持 IE11。
此迁移的原因是优先级的转变,其中一个或多个 Web 应用程序需要尽可能轻量且快速。因此,我们希望使用 Webpack 5 迁移到 Angular 11,完全放弃 IE,因此不再编译 ES5。
问题是我们的一些客户仍在其业务环境中使用 IE11。当我们检测到 IE11 时,我们已经显示一条使用现代浏览器的消息,但仅在使用 ES2015 时将不再显示此消息。
由于 IE11 不支持 ES2015,有没有办法向 IE11 用户显示一条消息以使用其他浏览器,而不仅仅是一个空白页面?
最佳答案
我以前也遇到过这样的问题。我检测浏览器版本,如果 IE 则呈现一些您需要的消息。如果不是 IE,则渲染应用程序。
我制作了一个新的 Angular 11 应用程序,并在 src/index.html 中使用 JavaScript 来检测浏览器,代码如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Angular11test</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<script type="text/javascript">
function get_browser() {
var ua = navigator.userAgent, tem, M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if (/trident/i.test(M[1])) {
tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
return { name: 'IE', version: (tem[1] || '') };
}
if (M[1] === 'Chrome') {
tem = ua.match(/\bOPR\/(\d+)/)
if (tem != null) { return { name: 'Opera', version: tem[1] }; }
}
if (window.navigator.userAgent.indexOf("Edge") > -1) {
tem = ua.match(/\Edge\/(\d+)/)
if (tem != null) { return { name: 'Edge', version: tem[1] }; }
}
M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];
if ((tem = ua.match(/version\/(\d+)/i)) != null) { M.splice(1, 1, tem[1]); }
return {
name: M[0],
version: +M[1]
};
}
var browser = get_browser()
var isSupported = isSupported(browser);
function isSupported(browser) {
var supported = false;
if (browser.name === "Chrome" && browser.version >= 48) {
supported = true;
} else if ((browser.name === "MSIE" || browser.name === "IE") && browser.version <= 11) {
supported = false;
} else if (browser.name === "Edge") {
supported = true;
}
return supported;
}
if (!isSupported) {
//render unsupported message
document.write("<h1>The app is not supported in IE. Please use other browsers!</h1>");
}
else{
//render app
var elem = document.createElement("app-root");
document.body.appendChild(elem);
}
</script>
</body>
</html>
关于javascript - Angular 11 与 Webpack 5 - 向 IE11 用户显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65682516/