Ark*_*osh 6 progressive-web-apps angular angular-pwa
我使用 Angular Guidelines创建了一个 PWA 。我在拦截应用安装横幅时遇到问题。我正在使用此代码将其推迟到稍后的时间:
let deferredPrompt;
window.addEventListener('beforeinstallprompt', (e) => {
// Prevent Chrome 67 and earlier from automatically showing the prompt
e.preventDefault();
// Stash the event so it can be triggered later.
deferredPrompt = e;
console.log("Intercepting the app install banner prompt");
setTimeout(function() {
deferredPrompt.prompt();
}, 20000);
// Wait for the user to respond to the prompt
deferredPrompt.userChoice
.then((choiceResult) => {
if (choiceResult.outcome === 'accepted') {
console.log('User accepted the A2HS prompt');
} else {
console.log('User dismissed the A2HS prompt');
}
deferredPrompt = null;
});
});
Run Code Online (Sandbox Code Playgroud)
我的清单文件:
{
"name": "TreadWill",
"short_name": "TreadWill",
"theme_color": "#2a3b3d",
"background_color": "#2a3b3d",
"display": "standalone",
"scope": "/",
"start_url": "/",
"icons": [
{
"src": "assets/icons/icon-72x72.png",
"sizes": "72x72",
"type": "image/png"
},
{
"src": "assets/icons/icon-96x96.png",
"sizes": "96x96",
"type": "image/png"
},
{
"src": "assets/icons/icon-128x128.png",
"sizes": "128x128",
"type": "image/png"
},
{
"src": "assets/icons/icon-144x144.png",
"sizes": "144x144",
"type": "image/png"
},
{
"src": "assets/icons/icon-152x152.png",
"sizes": "152x152",
"type": "image/png"
},
{
"src": "assets/icons/icon-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "assets/icons/icon-384x384.png",
"sizes": "384x384",
"type": "image/png"
},
{
"src": "assets/icons/icon-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
]
}
Run Code Online (Sandbox Code Playgroud)
当我在 localhost 中尝试此代码时,console.log 中包含的消息正在被记录,但 20 秒后,我收到错误消息:
Uncaught (in promise) DOMException
Run Code Online (Sandbox Code Playgroud)
在这一行:
deferredPrompt.prompt();
Run Code Online (Sandbox Code Playgroud)
当我托管代码并在移动设备上试用时,应用安装横幅会立即显示,而不是在 20 秒后显示。
我曾尝试将此代码放在 index.html 文件本身中,在单独的 js 文件中并在 index.html 文件中调用它。创建服务并在 .ts 文件中包含几乎相似的代码。没有任何效果。虽然我出于绝望尝试了 js 解决方案,但我更喜欢 Angular 解决方案来解决这个问题。理想情况下,我想在全局变量中捕获和存储 'beforeinstallprompt' 事件,并在不同的点提示该事件。
如何解决这个问题呢?
您可能做得正确,但根据这篇文章:
“当站点满足添加到主屏幕条件时,无论您是否在 beforeinstallprompt 事件上阻止Default(),迷你信息栏都会出现。”
所以对我来说,它也立即显现出来。
Pete LePage (@petele) 是一个在 Twitter 上关注 A2HS 更新的好人。
这是我构建的添加到主屏幕 (A2HS) 测试器。页面底部有一个源代码链接。请随意使用任何可能有帮助的东西。我最近没有将其更新到最新版本的 Angular。但它应该仍然可以正常工作,因为它是基本代码。
https://a2hs.glitch.me
| 归档时间: |
|
| 查看次数: |
3111 次 |
| 最近记录: |