Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Angular 9 CLI webworker中导入mathjs时出错_Angular_Angular Cli_Mathjs_Angular Devkit - Fatal编程技术网

在Angular 9 CLI webworker中导入mathjs时出错

在Angular 9 CLI webworker中导入mathjs时出错,angular,angular-cli,mathjs,angular-devkit,Angular,Angular Cli,Mathjs,Angular Devkit,我目前正在尝试使用Angular CLI 9.1.7将webworker添加到Angular 9项目中。 这个webworker应该使用mathjs 7运行一些计算 但是,将mathjs导入webworker始终会在浏览器控制台中导致以下错误。 如果我不导入mathjs,那么worker将毫无问题地运行 index.js:7 Uncaught ReferenceError: window is not defined at Object../node_modules/seed-rando

我目前正在尝试使用Angular CLI 9.1.7将webworker添加到Angular 9项目中。 这个webworker应该使用mathjs 7运行一些计算

但是,将mathjs导入webworker始终会在浏览器控制台中导致以下错误。 如果我不导入mathjs,那么worker将毫无问题地运行

index.js:7 Uncaught ReferenceError: window is not defined
    at Object../node_modules/seed-random/index.js (index.js:7)
    at __webpack_require__ (bootstrap:19)
    at Module../node_modules/mathjs/es/function/probability/util/seededRNG.js (seededRNG.js:1)
    at __webpack_require__ (bootstrap:19)
    at Module../node_modules/mathjs/es/function/probability/pickRandom.js (pickRandom.js:1)
    at __webpack_require__ (bootstrap:19)
    at Module../node_modules/mathjs/es/factoriesAny.js (factoriesAny.js:1)
    at __webpack_require__ (bootstrap:19)
    at Module../node_modules/mathjs/es/entry/pureFunctionsAny.generated.js (pureFunctionsAny.generated.js:1)
    at __webpack_require__ (bootstrap:19)
参考线为

var GLOBAL = typeof global === 'undefined' ? window : global;
并且是mathjs中使用的库的一部分。 我知道
window
在webworker中通常是未定义的,但是
global
应该根据我的理解进行设置。 在angular devkit内部使用的webpack配置中,我发现:

在那里,我已经尝试将
globalObject
设置为
self
this
在我的本地
node\u modules
文件夹中,但这并没有解决问题。 我在
angular.json
文件中也看不到为webworker提供任何额外配置来解决此问题的选项,因此我不确定如何才能使其正常工作。 在中,我发现了一个在webworker中使用它的示例,因此我假设也可以使用Angular CLI

我创建了一个简单的工作示例。如果你用

npm install
npm start
您可以在浏览器控制台中看到描述的错误

因此,我的问题是:如何在Angular CLI webworker中使用mathjs?

我在math.js GitHub上打开了一个窗口,其中提出了另一种解决方法: 只需将导入更改为

import { sqrt } from 'mathjs';

来修复webworker

这个解决方法相当简单,似乎很有效。但是,这个问题可能会在math.js的未来版本中得到解决。检查此问题是否有更新可能是有意义的

import { sqrt } from 'mathjs';
import { sqrt } from 'mathjs/dist/math.js';