簡體   English   中英

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

[英]Is it possible to run a Web Worker on my index.html in my angular app?

我的 angular 應用程序運行良好。 我只想在我的 index.html 的應用程序開頭添加一些計算,為了做到這一點,我在 index.html 文件的開頭添加了以下行:

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

myScript.js 文件與 index.html 位於同一目錄中,但它似乎無法識別它。 如果我 go 檢查該站點,我會看到以下錯誤:

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

任何人都知道為什么會這樣,我該如何解決? 我不知道它為什么要這樣尋找它。 任何幫助將不勝感激

我以前試過這個。 似乎它在 localhost:4200/myScript.js 中查找該文件但沒有找到它。 我嘗試啟動一個新服務器並直接從 localhost:5555/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. 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="module",然后您可以嘗試將自定義腳本移動到 src/asset 位置(或類似位置)並將文件添加到 angular.json 中的腳本部分。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM