使用给定的 tsconfig.json,
{
"compilerOptions": {
"lib": ["es2015", "dom"]
},
"files": [
"./1_webworker/tstut.ts",
"./1_webworker/worker.ts"
]
}
冉npm install --save @types/sharedworker
。
在下面的代码中(来自./1_webworker/tstut.ts
),
let worker: Worker = new Worker('worker.js');
使用 sharedworker 语法,声明:
let worker: SharedWorker.SharedWorker = new SharedWorker('worker.js');
但以下是错误。
worker.postMessage('do some work'); // Property 'postMessage' does not exist on type 'SharedWorker'
worker.addEventListener('message', (e) => {
console.log(e.data); // Property 'data' does not exist on type 'Event'
});
如何向共享工作者发布消息字符串?