appinstalled事件只能在PWA的安装程序中使用,在manifest.json文件中设置相应的信息。确保您的程序已被添加到主屏幕,并通过以下代码检查appinstalled事件是否被触发:
window.addEventListener('beforeinstallprompt', (e) => {
// Log the platforms provided as options in an install prompt
console.log(e.platforms); // ['web', 'android', 'windows']
e.preventDefault();
deferredPrompt = e;
showInstallPrompt();
});
window.addEventListener('appinstalled', (evt) => {
// Log installed
console.log('appinstalled', evt);
});