我第一次运行此脚本时,提示工作, 但是第二次正在运行我收到此错误:
install.js:27未捕获的TypeError:无法读取null的属性“提示” 在HTMLButtonElement.installPWA(install.js:27) 有人知道修复吗
let deferredInstallPrompt = null;
const installButton = document.getElementById('boutonInstall');
installButton.addEventListener('click', installPWA);
// Add event listener for beforeinstallprompt event
window.addEventListener('beforeinstallprompt', saveBeforeInstallPromptEvent);
/**
* Event handler for beforeinstallprompt event.
* Saves the event & shows install button.
*
* @param {Event} evt
*/
function saveBeforeInstallPromptEvent(evt) {
// CODELAB: Add code to save event & show the install button.
deferredInstallPrompt = evt;
installButton.removeAttribute('hidden');
//AJOUT de SHany
showInstallPromotion();
}
function installPWA(evt) {
// Add code show install prompt & hide the install button.
deferredInstallPrompt.prompt();
// Hide the install button, it can't be called twice.
evt.srcElement.setAttribute('hidden', true);
// Log user response to prompt.
deferredInstallPrompt.userChoice.then((choice) => {
if (choice.outcome === 'accepted') {
console.log('User accepted the A2HS prompt', choice);
} else {
console.log('User dismissed the A2HS prompt', choice);
}
deferredInstallPrompt = null;
});
}
// Add event listener for appinstalled event
window.addEventListener('appinstalled', logAppInstalled);
/**
* Event handler for appinstalled event.
* Log the installation to analytics or save the event somehow.
*
* @param {Event} evt
*/
function logAppInstalled(evt) {
// Add code to log the event
console.log('Hello World was installed.', evt);
}