Ale*_*nça 3 reactjs service-worker redux
我在我的应用程序中使用 React、Redux 和 Service Worker,我想在我的 Service Worker 中访问 Redux 存储。
我的服务人员是:
function checkCompatibility() {
if ('serviceWorker' in navigator && 'PushManager' in window) {
console.log('Service Worker and Push is supported');
navigator.serviceWorker.register('./sw.js')
.then(function (swReg) {
console.log('Service Worker is registered', swReg);
})
.catch(function (error) {
console.error('Service Worker Error', error);
});
} else {
console.warn('Push messaging is not supported');
}
}
function askForNotification() {
if (!('Notification' in window)) {
console.log('This browser does not support desktop notification');
} else if (Notification.permission === 'granted') {
console.log('Permission granted.');
} else if (Notification.permission !== 'denied' || Notification.permission === 'default') {
Notification.requestPermission(function (permission) {
if (permission === 'granted') {
var notification = new Notification('Permissão de Notificação foi Aceita', { icon: '/favicon.png', badge: '/favicon.png' });
}
});
}
}
checkCompatibility();
askForNotification();
Run Code Online (Sandbox Code Playgroud)
谁能帮我?
Service Worker 建立在 Web Worker API 之上。根据Web Workers 上的MDN 文档:
工人在不同于当前窗口的另一个全局上下文中运行
因此,服务工作者与您的 Redux 代码处于不同的范围内。
但是,您可以使用postMessage API 在主应用程序代码和服务工作者之间进行通信,并通过此通道从 Redux 传递您需要的任何数据。
看看这里的代码示例以获得灵感。
| 归档时间: |
|
| 查看次数: |
5280 次 |
| 最近记录: |