如何导入第三方库?在角

时间:2019-07-22 23:41:58

标签: node.js angular typescript

道歉的问题。我很少编写Typescript / Frontends的代码,而我陷入了Angular的困境。我想在Angular 7中使用node-html-parser libarary。我遵循了他们的文档,但是我从angular得到了一个错误:

  

错误TS2580:找不到名称'require'

我安装了npm install @ types / node,但是没有运气。我读了一些棱角分明的音符,但几乎没有类似的东西。

问题1。:如何将 node-html-parser 或其他第三方库导入Angular?

对于npm的3rd party库的相关内容,导入和处理,我深表感谢。 Stackoverflow仅回答地址碎片。

这是一个给我带来问题的代码段:

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { parse } from 'node-html-parser';

@Component({
  selector: 'run',
  templateUrl: './run.component.html',
  styleUrls: ['./run.component.css']
})
export class RunComponent implements OnInit {

  constructor(private http: HttpClient) { }

  HTMLParser = require('node-html-parser');

  run() {
    var root = this.HTMLParser.parse( /* ... */);
  }

  ngOnInit(): void {
  }  
}

我的package.json:

{
  "name": "retriever",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~7.2.0",
    "@angular/common": "~7.2.0",
    "@angular/compiler": "~7.2.0",
    "@angular/core": "~7.2.0",
    "@angular/forms": "~7.2.0",
    "@angular/platform-browser": "~7.2.0",
    "@angular/platform-browser-dynamic": "~7.2.0",
    "@angular/router": "~7.2.0",
    "@types/axios": "^0.14.0",
    "@types/node": "^12.6.8",
    "axios": "^0.19.0",
    "core-js": "^2.5.4",
    "fast-html-parser": "^1.0.1",
    "node-html-parser": "^1.1.16",
    "rxjs": "~6.3.3",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.13.0",
    "@angular/cli": "~7.3.8",
    "@angular/compiler-cli": "~7.2.0",
    "@angular/language-service": "~7.2.0",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "~4.5.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~3.2.2"
  }
}

最后是tsconfig-添加了 types:节点

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "types": [ "node "],
    "lib": [
      "es2018",
      "dom"
    ]
  }
}

谢谢

1 个答案:

答案 0 :(得分:0)

您已经有parse。您只需要直接调用它即可:

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { parse } from 'node-html-parser';

@Component({
  selector: 'run',
  templateUrl: './run.component.html',
  styleUrls: ['./run.component.css']
})
export class RunComponent implements OnInit {

  constructor(private http: HttpClient) { }

  run() {
    var root = parse( /* ... */);
  }

  ngOnInit(): void {
  }  
}

查看Usage Section for node-html-parser以获得更多信息。

  

这是您推荐的Working CodeSandbox