Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.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 2组件中使用npm包_Angular_Npm_Package - Fatal编程技术网

在Angular 2组件中使用npm包

在Angular 2组件中使用npm包,angular,npm,package,Angular,Npm,Package,我想在我的Angular 4组件中使用一个可用的npm包,但我很难让它工作。该软件包称为“ip”,可从 我添加了以下程序包: import { isV4Format } from 'ip'; export class TabValidator { private static _check(address: string): any { const ip = require('ip'); if (!myip.isV4Format(address)) {

我想在我的Angular 4组件中使用一个可用的npm包,但我很难让它工作。该软件包称为“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;
    }
}
npm安装ip--保存

npm安装--保存@types/ip

在我的app.module.ts中,我有:

import { isV4Format } from 'ip';
我将isV4Format添加到app.module.ts中的导入列表中

然后,我尝试在我的课堂上使用ip包it,如下所示:

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;
    }
}
编译器抱怨组件:

找不到名称“require”

关于app.module.ts:

类型为“{declarations:(接口组件的类型| EditnicComponent的类型| AppComponent的类型)[];i..”的参数不能分配给类型为“NgModule”的参数


有人能告诉我上面的问题是什么吗?我怀疑有一些地方错了。我在SO上发现了两个类似的问题,但它们似乎适用于不同的角度版本。

在SO上有很多错误/过时的答案,包括这些错误的答案:

似乎Angular使它变得更容易;只需将此项添加到要使用节点模块的组件中:

export declare let require: any;
const ip = require('ip');

现在,这可能不是实现您想要的目标的最佳方式,因为我在Angular中读到了一些关于使用“require”会对代码共享或类似内容产生负面影响的内容。但如果有人能给出更好的答案,我会接受的!

我尝试了其他地方推荐的“npm install@types/node”,但这无助于尝试将require块向右移动像这样的低导入:
import…const ip=require('ip');
然后在函数中使用它。如果没有帮助,那么在require块中写入js的路径,像
const Highcharts=require('Highcharts/highstock');