angularjs – 在angular.js应用程序中注册服务工作者

前端之家收集整理的这篇文章主要介绍了angularjs – 在angular.js应用程序中注册服务工作者前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用ionic和angular.js创建一个应用程序,我在注册一个服务工作者时遇到了困难,我打算用它来添加新的应用程序安装横幅功能.我按照指示在我的app.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

You’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);
  });
原文链接:https://www.f2er.com/angularjs/143709.html

猜你在找的Angularjs相关文章