如何将第3方npm模块/软件包添加到angular 8应用

时间:2019-11-25 17:13:43

标签: angular typescript npm angular8

因此,我是Angle和Typescript的完全新手,因此对于社区来说这很容易。我已经在网上冲浪了好几个小时,但仍然没有找到有关如何在我的角度应用程序中添加第三方模块或软件包的简单指南。

问题:我正在尝试编写一种rss阅读器。我发现可以通过安装此npm软件包来完成

https://www.npmjs.com/package/rss-to-json

我确实已经尝试了很多东西,但是还是没有运气。我将向您展示我目前正在使用的最新代码。

http-requests.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { HttpHeaders } from '@angular/common/http';
import { Feed } from 'rss-to-json';

@Injectable({
  providedIn: 'root'
})

export class HttpRequestsService {

  constructor(private http: HttpClient) { }

  doDummyRequest() {
    return Feed.load('https://learnstartup.net/feed/', function(err, rss){
      console.log(rss);
    });
    // return this.http.get("https://cors-anywhere.herokuapp.com/http://rss.cnn.com/rss/edition.rss");
  }

  displayDummyRequest() {
    this.doDummyRequest().subscribe((data: any) => {
      console.log(12346);
      console.log(data);
    });
  }
}

此代码返回

RssFeedComponent.ngfactory.js? [sm]:1 ERROR TypeError: Cannot read property 'load' of undefined
    at HttpRequestsService.doDummyRequest (http-requests.service.ts:15)
    at RssFeedComponent.onDummyButtonClick (rss-feed.component.ts:18)
    at Object.eval [as handleEvent] (RssFeedComponent.html:5)
    at handleEvent (core.js:43993)
    at callWithDebugContext (core.js:45632)
    at Object.debugHandleEvent [as handleEvent] (core.js:45247)
    at dispatchEvent (core.js:29804)
    at core.js:42925
    at HTMLButtonElement.<anonymous> (platform-browser.js:2668)
    at ZoneDelegate.invokeTask (zone-evergreen.js:391)

我的IDE(我使用Visual Studio Code)也显示了一个提示

Could not find a declaration file for module 'rss-to-json'. '/home/h8machine/App/news-agg/node_modules/rss-to-json/dist/index.js' implicitly has an 'any' type.
  Try `npm install @types/rss-to-json` if it exists or add a new declaration (.d.ts) file containing `declare module 'rss-to-json';`ts(7016)

将感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

rss-to-json是NodeJ的特定库,因此您将无法在浏览器中使用它。