在 serviceWorker
推送承诺解决之前,无法使用 importScript
解决依赖关系;有人可以提出更好的方法来排序吗?
ServiceWorker fetch
事件:
var iDB; //全局变量
 self.addEventListener(“fetch”,function(e){
 self.importScripts(“dexie.min.js”);
 var o = new Dexie('database_name' );
 o.version(1).stores({
 iDBStore:“++ id,adspotkey”
 iDB = o
});
});& #xA;
&#xA;&#xA; 服务工作者<代码>推送活动
&#xA;&# xA; self.addEventListener(“push”,function(e){&#xA; e.waitUntil(getDetails()。then(function(ex){&#xA; try {&#xA; e = wpSDK.iDB;&#xA;返回e.iDBStore.where(“id”)。above(0).toArray()。then(function(ox){&#xA; console.log(ox); && #xA;}&#xA;}));&#xA;});&#xA;
&#xA;&#xA; 注意:
&#xA;&#xA; push
控制台抛出错误,如下所示&#xA; TypeError:无法读取属性' iDBStore'未定义(...)
答案 0 :(得分:1)
你应该像这样在后台运行importScripts
。
self.importScripts("dexie.min.js");
var iDB; // Global Variable
self.addEventListener("fetch", function(e) {
var o = new Dexie('database_name');
o.version(1).stores({
iDBStore: "++id,adspotkey"
iDB = o
});
});
importScripts
是一种同步方法。因此,当您在后台运行此方法时,它将阻止ServiceWorker处于活动状态,直到脚本完全加载为止。