简体   繁体   English

是否可以在我的 angular 应用程序中的 index.html 上运行 Web Worker?

[英]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.

相关问题 Angular:我的 app.component.html 没有注入 index.html<app-root> - Angular: my app.component.html is not injecting into index.html <app-root> 在 Angular 渐进式 web 应用程序中强制重新加载远程索引。 - Enforcing reload of remote index.html in an Angular progressive web app 我的/index.html重定向到/ [object Object] - my /index.html is redirecting to /[object Object] 当我运行 npm 启动时,我的 webpack 找不到我的索引。html 页面 - my webpack cannot find my index.html page when I run npm start 为什么我的Angular $ http get请求返回我的index.html? - Why does my Angular $http get request return my index.html? 使用 API 中的 HTML 而不是 index.html 中的静态 HTML 来为我的 React 应用程序补充水分 - Hydrate my React app with HTML from the API instead of static HTML from index.html 将 DOCTYPE html 添加到我的 index.html 会破坏我的 javascript - Adding DOCTYPE html to my index.html breaks my javascript 我的网站加载出现问题。 这是嵌入到index.html中的.SWF文件 - Having an issue with my Web site loading. This is a .SWF file embedded into the index.html 无法从angular2的查看文件夹中呈现index.html文件 - Unable to render index.html file from my view folder in angular2 我如何在没有Index.html或.php的情况下将WebApplication文件运行到服务器? - How can I run my WebApplication files to the server without an Index.html or.php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM