我有一个困难的时间检查未定义的pwa安装提示()。我试着基于下面的代码进行检查,但是我不能让它工作。第一个片段是我如何设置监听器的:
`let deferredPrompt;
window.addEventListener('beforeinstallprompt', evt => {
evt.preventDefault();`enter code here`
deferredPrompt = evt;
});`然后在一个单独的函数中,我尝试用两种不同的方法进行检查,但是如果提示符是未定义的,我就无法捕捉到这种情况
`if(deferredPrompt !== null && deferredPrompt !== 'undefined') { // chromium browser exists
promptBanner.style.display = 'none';
// Show Chrome a2hs modal
deferredPrompt.prompt().then(() => {
deferredPrompt.userChoice.then((choiceResult) => {
if (choiceResult.outcome === 'accepted') {
console.log('User accepted the install prompt');
} else {
console.log('User dismissed the install prompt');
deferredPrompt = null;
}
});
}).catch(err => {
console.log("Prompt is not available" + err);
});` 有谁有什么建议。谢谢,我希望你们都平安!佩里
发布于 2020-04-29 08:20:10
首先使用以下代码侦听beforeinstallprompt事件。
window.addEventListener("beforeinstallprompt", (event) => {
console.log("beforeinstallprompt fired -> ", event);
window.deferredPrompt = event;
document.getElementById("yourInstallButtonContainer").classList.toggle("hidden", false);
});现在,您可以使用以下代码处理install按钮的单击。
const promptEvent = window.deferredPrompt;
if (!promptEvent) return;
promptEvent.prompt();
promptEvent.userChoice.then((choice) => {
console.log("User selected -> ", choice);
window.deferredPrompt = null;
document
.getElementById("yourInstallButtonContainer")
.classList.toggle("hidden", true);
});如果您的promptEvent未定义,则可以记录一条语句。如果!promptEvent为true,则上述代码将返回
https://stackoverflow.com/questions/61333222
复制相似问题