1.采用原生的引入方式 直接引入web worker ,总是报路径错误
const isDev = process.env.NODE_ENV === 'development'
console.log(process)
console.log(666)
console.log(process.env.NODE_ENV)
console.log(process.env)
const worker_js_path = isDev
? path.join(__dirname, 'worker.js')
: path.join(process.resourcesPath, 'app.asar.unpacked/source/worker.js')
const worker = new Worker(worker_js_path)
if (worker) {
worker.postMessage('12344')
worker.onmessage = event => {
this.getData(event.data.originData, event.data.allDataArray)
}
worker.onerror = function (error) {
console.log(444, error, error.message)
worker.terminate()
}
}
GET file:///C:/Users/fugang/Desktop/Warehouse%20management%20system/wareHouse-manager/node_modules/electron/dist/resources/electron.asar/renderer/worker.js net::ERR_FILE_NOT_FOUND
2.采用worker loader的方式引入web worker
注意:这里有个坑 测试环境按照网上的跑起来没有问题
但是打包之后始终报错误 Uncaught TypeError: Cannot read properties of undefined (reading 'webpackJsonp')
//针对于这个错误搜索出来的解决方法 只有针对vue-cli2的webpack.prod.conf.js,没有针对vue-cli3的vue.config.js解决方法
3.没有办法只能通过 inline-worker引入试试
npm install inline-worker 地址 https://github.com/mohayonao/inline-worker
原理是把对应的脚本文件转换成二进制 BLOB
上面2种方法搞了半天硬是没有找到答案