angular - 如何修复:“数学”类型上不存在属性“种子随机”
问题描述
我试图将seedrandom添加到 Angular项目中。我已经使用 npm 安装了它和 typescript 定义。我的 json 包有相关条目(如下),所以我确信它安装正常,并且相关文件夹位于节点模块中。
"dependencies": {
"@types/seedrandom": "^2.4.28",
"seedrandom": "^3.0.5"
}
尽管如此,我还是一直收到错误消息:"Property 'seedrandom' does not exist on type 'Math'."
事实上,当我这样做时ng serve
,控制台中会抛出错误,但网络应用程序工作正常。当我这样做时,ng build
我收到错误并且构建失败。
我已经尝试过为此定义一个接口,但没有帮助。我也盯着代码看了两天,也没有用。
getRandomNumber(n) {
const randomNumber = new Math.seedrandom( '1234' );
return Math.round( Math.round( randomNumber() * n) );
}
更新:根据 Myk 的回答,我尝试了以下方法:
import { seedrandom } from 'seedrandom';
getRandomNumber(n) {
const randomNumber = seedrandom( '1234' );
return Mathround( Math.round( randomNumber() * n) );
}
只得到错误:Module '"../blah/node_modules/@types/seedrandom"' has no exported member 'seedrandom'.
这很有趣,因为当我检查 @types/seedrandom/index.d.ts 文件时包含以下行:
export = seedrandom;
export as namespace seedrandom;
如何让种子随机在 Angular 中工作?
解决方案
根据文档:
从版本 3 开始,当通过 require('seedrandom') 使用时,全局 Math.seedrandom 不再可用。
因此,如果您希望在通过 安装的 Angular 应用程序中使用它npm
,您应该直接导入该函数并使用它返回的随机数生成器:
const seedrandom = require('seedrandom');
const rng = seedrandom('<seed>');
推荐阅读
- excel - Convert stock transactions into daily holdings in Excel
- javascript - using uibModal in angularJS v1.48 applcation
- dart - Dart - Hide method from parent class / call private method from parent class
- javascript - handle find property in array if undefined
- javascript - Uncaught TypeError: downloadButton is null
- javascript - Change List of objects in position using map for Reactjs
- python - Pytest在导入模块时需要指定文件夹项目
- python - 如何使用 Python 和 Selenium 将多个文件上传到网站?
- r - 方差分析公式无法识别我的列标题
- javascript - 悬停按钮时更改多个样式属性