将work导出,不用打包外之后的地址引入
worker_script.js:
const workerCode = () => { self.onmessage = (e) => { setInterval(() => { // 自己的逻辑代码 },e.data); }; }; let code = workerCode.toString(); code = code.substring(code.indexOf('{') + 1,code.lastIndexOf('}')); const blob = new Blob([code],{ type: 'application/javascript' }); const worker_script = URL.createObjectURL(blob); export default worker_script;
在其他界面进行引入:
import worker_script from "./worker_script"; let worker = new Worker(worker_script); worker.postMessage(); worker.onmessage = e => { };