我想在我的Angular 4组件中使用一个可用的npm包,但我正在努力使其工作 . 该包名为'ip',可从https://www.npmjs.com/package/ip获取
我添加了如下包:
npm install ip --save npm install --save @ types / ip
在我的app.module.ts我有:
import { isV4Format } from 'ip';
我将isV4Format添加到app.module.ts中的导入列表中
然后我尝试在我的课程中使用ip包,如下所示:
import { isV4Format } from 'ip';
export class TabValidator {
private static _check(address: string): any {
const ip = require('ip');
if (!myip.isV4Format(address)) { return { 'wrongFormat': true }; }
return null;
}
}
编译器抱怨组件:
找不到名字'要求'
和app.module.ts:
类型'{声明:(typeof InterfacesComponent | typeof EditnicComponent | typeof AppComponent)[]的参数; i ...'不能分配给'NgModule'类型的参数 .
有人可以告诉我上面有什么问题吗?我怀疑有一些问题 . 我在SO上发现了两个类似的问题,但它们似乎是针对不同的角度版本 .
1 回答
SO上有很多错误/过时的答案,包括错误的答案:
Angular似乎让它更容易;只需将其添加到要使用节点模块的组件中:
现在这可能不是实现你想要的最好的方法,因为我读到了一些关于使用'require'会对代码共享产生负面影响或类似Angular的东西 . 但如果有人可以发布更好的答案我会接受!