gpt4 book ai didi

angular - 如何在 Angular PWA 中拦截应用程序安装提示?

转载 作者:太空狗 更新时间:2023-10-29 18:22:06 26 4
gpt4 key购买 nike

我使用 Angular 创建了一个 PWA guidelines .我在拦截应用程序安装横幅时遇到问题。我正在使用此代码将其推迟到以后:

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;
});
});

我的 list 文件:

{
"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"
}
]
}

当我在本地主机上尝试此代码时,包含在 console.log 中的消息被记录下来,但 20 秒后,我收到错误:

Uncaught (in promise) DOMException

在这一行中:

deferredPrompt.prompt();

当我托管代码并在移动设备上试用时,应用安装横幅会立即显示,而不是 20 秒后显示。

我试过将这段代码放在 index.html 文件本身,一个单独的 js 文件中,然后在 index.html 文件中调用它。创建服务并在 .ts 文件中包含几乎相似的代码。没有任何效果。尽管我出于绝望尝试了 js 解决方案,但我更喜欢 Angular 解决方案来解决这个问题。理想情况下,我想捕获“beforeinstallprompt”事件并将其存储在全局变量中,并在不同点提示该事件。

如何解决这个问题?

最佳答案

您可能做得很好,但根据 this article :
“当网站满足添加到主屏幕的条件时,迷你信息栏将出现,不管您是否在 beforeinstallprompt 事件中使用 preventDefault()。”
所以对我来说,它也会立即显示出来。

Pete LePage (@petele) 是一个在 Twitter 上关注 A2HS 更新的好人。

这是我构建的添加到主屏幕 (A2HS) 测试器。页面底部有一个指向源代码的链接。随意使用任何可能有用的东西。我最近没有更新到最新版本的 Angular 。但它应该仍然可以正常工作,因为它是基本代码。
https://a2hs.glitch.me

关于angular - 如何在 Angular PWA 中拦截应用程序安装提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54673405/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com