首页 > 解决方案 > 如何导入 3-rd 方库?在角

问题描述

为琐碎的问题道歉。我很少编写 Typescript/Frontends,而且我被 Angular 困住了。我想在 Angular 7 中使用 node-html-parser 库。我遵循了他们的文档,但是我从 Angular 中得到一个错误:

错误 TS2580:找不到名称“要求”

我安装了 npm install @types/node 但没有运气。我浏览了我的角度笔记,但很少有这样的东西。

问题 1.:如何将node-html-parser或其他 3rd 方库导入 Angular?

我希望能从 npm 中详细了解这些内容、导入和处理 3rd 方库。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 - 添加类型:节点

{
  "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"
    ]
  }
}

谢谢

标签: node.jsangulartypescript

解决方案


你已经有了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 {
  }  
}

查看node-html-parser的用法部分以获取更多信息。

这是您的参考的工作 CodeSandbox


推荐阅读