数学'The global config is read-only'?

Mathjs 'The global config is read-only'?

我正在使用 Angular 8 和 math.js 库来处理大数字。

我刚刚有 math.js 的更新版本,从 5.4.0 到 6.2.3。

我以这种方式在我的组件中使用 math.js:

import * as mathjs from 'mathjs';

constructor() {
   mathjs.config({ number: 'BigNumber', precision: 128 });
}

更新后突然出现新错误

Error: The global config is readonly. Please create a mathjs instance if you want to change the default configuration. Example:

import { create, all } from 'mathjs';
const mathjs = create(all);

mathjs.config({ number: 'BigNumber' });

我试过import { create, all } from 'mathjs,但是那些方法根本不存在

这个问题的解决方法是什么?

不确定是否能解决您的问题,但根据错误,您应该创建一个新实例并稍后使用,如下所示 -

import { create, all } from 'mathjs';
  const mathjs = new create(all);
  mathjs.config({ number: 'BigNumber' });

经过一段时间我终于找到了解决方案。

首先我需要删除行:

import * as mathjs from 'mathjs';

此行不再进行降神会,因为我们需要使用此名称创建变量,这将是具有新配置的 mathjs 的新实例。

import { create, all, MathJsStatic } from 'mathjs';

private mathjs: Partial<MathJsStatic>;

  constructor() {
    this.mathjs = create(all, { number: 'BigNumber', precision: 128 });
  }

如果我们需要在所有应用程序中使用相同的 mathjs 配置,最好的方法是创建一个服务并在所有地方使用相同的实例。