[英]Service Worker PostMessage Error
I have the following: 我有以下内容:
//main.js
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('worker.js').then(function(worker){
// firebase.messaging().useServiceWorker(worker);
navigator.serviceWorker.controller.postMessage([MESSAGE HERE]);
});
}
And in worker.js: 在worker.js中:
self.onmessage=function(e){
console.log(e.data);
}
But in main.js, I get this error: 但是在main.js中,出现此错误:
Uncaught TypeError: Cannot read property 'postMessage' of null
未捕获的TypeError:无法读取null的属性'postMessage'
Why is this happening? 为什么会这样呢?
This is because you are going into a new function with the line 这是因为您将使用该行进入新功能
navigator.serviceWorker.register('worker.js').then(function(worker){
and passing in your service worker as 'worker' 并将您的服务人员称为“工人”
you should be able to replace 您应该能够更换
navigator.serviceWorker.controller.postMessage([MESSAGE HERE]);
with 与
worker.active.postMessage('your message');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.