[英]Is it possible to run a Web Worker on my index.html in my angular app?
I have my angular app working fine.我的 angular 应用程序运行良好。 I just want to add some computations at the beginning of the app on my index.html in order to do that I added the following line at the beginning of that index.html file:
我只想在我的 index.html 的应用程序开头添加一些计算,为了做到这一点,我在 index.html 文件的开头添加了以下行:
<script type="module" src="myScript.js"></script>
The myScript.js file is in the same directory as the index.html But it seems like it does not recognize it. myScript.js 文件与 index.html 位于同一目录中,但它似乎无法识别它。 If I go to inspect the site I can see the following error:
如果我 go 检查该站点,我会看到以下错误:
GET http://localhost:4200/myScripts.js 404 (Not Found)
Anyone knows why that is and how can I fix it?任何人都知道为什么会这样,我该如何解决? I do not know why it is looking for it like that.
我不知道它为什么要这样寻找它。 Any help would be greatly appreciated
任何帮助将不胜感激
I tried this before.我以前试过这个。 It seems like it looks for the file in localhost:4200/myScript.js and does not find it.
似乎它在 localhost:4200/myScript.js 中查找该文件但没有找到它。 I have tried spinning up a new server and calling it directly from localhost:5555/myScript.js and it is able to find it.
我尝试启动一个新服务器并直接从 localhost:5555/myScript.js 调用它,它能够找到它。 but it does not run it: It gives me the error:
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.
但它没有运行它:它给了我错误:
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.
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.
Here is what I tried这是我尝试过的
<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');
}
Try without type="module", then you can try to move the custom script to a src/asset location (or something like that) and add the file to scripts section in angular.json.尝试不使用 type="module",然后您可以尝试将自定义脚本移动到 src/asset 位置(或类似位置)并将文件添加到 angular.json 中的脚本部分。
"scripts": [
"src/assets/custom.js"
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.