2

我试图在 Typescript 中使用 WebWorkers,但遇到了一个问题。我已经安装了@types/node 并更新了所有内容。'worker_threads' 不再是实验性的,并且处于 node.js 的稳定版本中。

这是我的打字稿配置:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "baseUrl": ".",
    "types": [
      "webpack-env",
      "node",
      "dom-mediacapture-record"
    ],
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}

当我尝试像这样在我的打字稿文件中导入时:

import { Worker } from 'worker_threads';

我收到错误:


This dependency was not found:

* worker_threads in ./src/SpeechRecognizer/SpeechRecognizer.ts

To install it, you can run: npm install --save worker_threads
Type checking and linting in progress...
No type errors found
No lint errors found
Version: typescript 3.6.3, tslint 5.20.0

我也使用 Vue.js 作为前端。我已经尝试了我能找到的一切,所以我很高兴得到每一个帮助。如果这不起作用,我将适当地切换到 JavaScript。谢谢。

4

1 回答 1

0

你可以使用 npm 包'worker-loader'。转到以下链接https://www.npmjs.com/package/worker-loader并参考“与 TypeScript 集成”部分

于 2019-10-06T17:30:23.483 回答