简体   繁体   English

Angular 6中的xml2js:无法解析'stream'和'timers'

[英]xml2js in Angular 6: Can't resolve 'stream' and 'timers'

I would like to parse xml data, retrieved from the server. 我想解析从服务器检索的xml数据。 Unfortunately HttpClient does not support xml, only json, therefore I installed the package xml2js : 不幸的是HttpClient不支持xml,只支持json,因此我安装了包xml2js

npm install xml2js --save
npm install @types/xml2js --save-dev

Then I try to use it like this: 然后我尝试像这样使用它:

import {Parser} from 'xml2js';

Parser.parseString(xml_str, function (err, result) {
    console.dir(result);
});

I get these two errors if I run it: 如果我运行它,我会收到这两个错误:

WARNING in ./node_modules/xml2js/node_modules/sax/lib/sax.js
Module not found: Error: Can't resolve 'stream' in 'C:\projects\app\node_modules\xml2js\node_modules\sax\lib'

ERROR in ./node_modules/xml2js/lib/parser.js
Module not found: Error: Can't resolve 'timers' in 'C:\projects\app\node_modules\xml2js\lib'

I haven't found any solutions to this problem, maybe it is an Angular6 issue only. 我没有找到任何解决这个问题的方法,也许它只是一个Angular6问题。 Is there any way, to parse xml in Angular6? 有没有办法解析Angular6中的xml?

You'll have to install those dependencies. 您必须安装这些依赖项。 It's unfortunately not well documented. 遗憾的是,没有详细记录。

npm install --save stream timers

For Angular to recognise xml2js, the following steps are required: 要使Angular识别xml2js,需要执行以下步骤:

  1. Add the timers-browserify node module using "npm install timers-browserify" 使用“npm install timers-browserify”添加timers-browserify节点模块
  2. Add the stream-browserify node module using "npm install stream-browserify" 使用“npm install stream-browserify”添加stream-browserify节点模块
  3. Add the following path mapping in your tsconfig.json: 在tsconfig.json中添加以下路径映射:
    "compilerOptions": {
      "paths": {
        "timers": [
          "node_modules/timers-browserify"
        ],
        "stream": [
          "node_modules/stream-browserify"
        ],
      }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM