我的应用程序在离子4角下。 我已经用:
安装了pwa部分。ng add @angular/pwa --project app
然后使用:ionic build --prod
并使用firebase deploy
但是我有两个问题:
1)当我从Android手机浏览应用程序时,未显示横幅“添加到屏幕”。 即使在根url上有此代码:
showBtn: boolean = false;
deferredPrompt;
constructor(private modalController: ModalController, public authUser: AuthUserService, private router: Router){}
ionViewWillEnter(){
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 on the button event.
this.deferredPrompt = e;
// Update UI by showing a button to notify the user they can add to home screen
this.showBtn = true;
});
//button click event to show the promt
window.addEventListener('appinstalled', (event) => {
alert('installed');
});
if (window.matchMedia('(display-mode: standalone)').matches) {
alert('display-mode is standalone');
}
}
2)当我进行灯塔审核时,收到此警告:
Does not register a service worker that controls page and start_url
我尝试卸载,重新安装,重建所有内容,但没有任何效果。 在离子文档上,我找不到解决此问题的任何线索。
答案 0 :(得分:0)
几天后,我能够使它起作用。
首先,我将以下代码段添加到firebase.json文件的托管属性中:
{
"source": "ngsw-worker.js",
"headers": [
{
"key": "Cache-Control",
"value": "no-cache"
}
]
}
然后我将此脚本添加到index.html中:
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('ngsw-worker.js')
.then(() => console.log('service worker installed'))
.catch(err => console.error('Error', err));
}
</script>
现在可以了!