有可能在我的angular应用程序中的index.html上运行Web Worker吗



我的angular应用程序运行良好。我只想在我的index.html应用程序的开头添加一些计算,为了做到这一点,我在index.html文件的开头添加了以下行:

<script type="module" src="myScript.js"></script>

myScript.js文件与index.html位于同一目录中,但它似乎无法识别它。如果我去检查网站,我可以看到以下错误:

GET http://localhost:4200/myScripts.js 404 (Not Found)

有人知道为什么会这样,我该怎么解决?我不知道它为什么要这样找它。如有任何帮助,将不胜感激

我以前试过这个。它似乎在localhost:4200/myScript.js中查找文件,但没有找到。我试着启动一个新服务器,并直接从localhost:55555/myScript.js调用它,它能够找到它!但它没有运行它。它给了我错误:Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.

以下是我尝试的

<script type="module">
if (window.Worker) {
console.log('index');
const worker = new Worker('http://localhost:5555/app.worker.ts', {type: 'module'});
worker.onmessage = ({ data }) => {
console.log(`page got message: ${data}`);
};
worker.postMessage('helloMatcha');
}

尝试不使用type="模块";,然后,您可以尝试将自定义脚本移动到src/asset位置(或类似的位置(,并将该文件添加到angular.json.中的脚本部分

"scripts": [
"src/assets/custom.js"
]

最新更新