繁体   English   中英

ReferenceError:xml2js 中未定义全局

[英]ReferenceError: global is not defined in xml2js

我想将 XML 字符串转换为 JSON 以获取一些用户详细信息。 我正在使用 xml2js 库。 下面是我的打字稿代码:

打字稿.ts

sendXML(){
    console.log("Inside sendXML method")
    console.log(this.XMLAsString)
    this.parseXML(this.XMLAsString);
}

parseXML(XMLAsString){
    var result;
    var parser=require('xml2js');
    parser.Parser().parseString(XMLAsString, (err, r)=>{result=r});
    console.log(result);
  }

ItrAgainstOrderFilTypeComponent.html

<button class="largeButton primaryButton" [disabled]="proceedToVerification" (click)="sendXML()">Proceed to Submit</button>

sendXML()调用解析提供的 String XMLAsString parseXML()函数。 parseXML()将字符串作为参数,并应打印其等效的 JSON。

但是当我尝试运行此代码时,出现以下错误: ItrAgainstOrderFilTypeComponent.html:112 ERROR ReferenceError: global is not defined

这是我第一次使用xml2js lib。我该如何解决这个问题? 另外,为什么在 html 文件中显示错误(错误在上面提供的 html 中)?

看起来您正试图在浏览器中require一个包,这通常是在 Node.js 脚本中导入包的方式。

您可能需要找到另一个库或使用Browserify

暂无
暂无

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

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