js应用服务工作者

js应用服务工作者

本文介绍了注册在angular.js应用服务工作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的离子和angular.js创建一个应用程序,我在其中注册我打算使用添加新的应用程序安装的横幅功能的服务人员的困难。我加入我的app.js文件低于code的指示,但我注意到获得登记,也没有发生任何错误的信号。

这是code我加入到我的app.js:

 如果(在导航'serviceWorker'){
  navigator.serviceWorker.register('服务worker.js'),然后(功能(注册){
    //注册成功
    的console.log('ServiceWorker注册成功的范围:,registration.scope);
  })赶上(功能(错误){
    //注册失败 :(
    的console.log('ServiceWorker注册失败:',ERR);
  });
}


解决方案

请确保在本地主机你要么加载页面或者你必须使用 HTTPS

https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker_API/Using_Service_Workers

Check if your Browser supports this feature.

if ('serviceWorker' in navigator) {
  [...]
} else {
  console.log("this browser does NOT support service worker");
}

This might help: http://caniuse.com/#feat=serviceworkers

If you want to see the current state of your serviceworker you could do something like this:

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);
  });

这篇关于注册在angular.js应用服务工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 02:19