我正在使用ionic和angular.js创建一个应用程序,我在注册一个服务工作者时遇到了困难,我打算用它来添加新的应用程序安装横幅功能.我按照指示在我的app.js文件中添加以下代码,但我注意到发生了注册的任何信号,也没有任何错误.
if ('serviceWorker' in navigator) { navigator.serviceWorker.register('service-worker.js').then(function(registration) { //Registration was successful console.log('ServiceWorker registration successful with scope: ',registration.scope); }).catch(function(err) { //registration Failed :( console.log('ServiceWorker registration Failed: ',err); }); }
确保您在localhost上加载页面,或者必须使用https
原文链接:https://www.f2er.com/angularjs/143709.htmlYou’ll also need to serve your code via HTTPS — Service Workers are restricted to running across HTTPS for security reasons. GitHub is therefore a good place to host experiments,as it supports HTTPS.
https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker_API/Using_Service_Workers
if ('serviceWorker' in navigator) { [...] } else { console.log("this browser does NOT support service worker"); }
这可能会有所帮助:http://caniuse.com/#feat=serviceworkers
如果您想查看您的服务工作者的当前状态,您可以执行以下操作:
navigator.serviceWorker.register('/serviceworker.js',{scope: '/'}) .then(function (registration) { var serviceWorker; if (registration.installing) { serviceWorker = registration.installing; } else if (registration.waiting) { serviceWorker = registration.waiting; } else if (registration.active) { serviceWorker = registration.active; } if (serviceWorker) { console.log("ServiceWorker phase:",serviceWorker.state); serviceWorker.addEventListener('statechange',function (e) { console.log("ServiceWorker phase:",e.target.state); }); } }).catch(function (err) { console.log('ServiceWorker registration Failed: ',err); });