在Angular 2中使用JS库xml2js [英] Use JS library xml2js with Angular 2

查看:161
本文介绍了在Angular 2中使用JS库xml2js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Angular 2(带有TypeScript的RC 1)Web应用程序中使用xml2js作为XML解析器.但是我只得到一些错误,没有可行的解决方案.

I´m trying to use xml2js as XML parser within an Angular 2 (RC 1 with TypeScript) webapplication. However I only get some errors and no working solution.

这是我逐步执行的操作:

Here is what I did step by step:

  • 通过npm install xml2js
  • 安装xml2js
  • 通过tsd install xml2js
  • 根据d.ts文件安装
  • 包含到我的index.html的脚本链接:<script src="../node_modules/xml2js/lib/xml2js.js"></script>
  • 通过添加'xml2js': '../node_modules/xml2js'到映射和'xml2js': { defaultExtension: 'js' }到包
  • 配置SystemJS
  • 通过import {Parser} from 'xml2js';
  • 在我的组件中导入xml2js的解析器
  • 尝试在组件的构造函数中使用它:
    var parser = new Parser(); var xml = "<root>Hello xml2js!</root>" parser.parseString(xml, function (err, result) { console.dir(result); });
  • installed xml2js via npm install xml2js
  • installed according d.ts-file via tsd install xml2js
  • included script link to my index.html: <script src="../node_modules/xml2js/lib/xml2js.js"></script>
  • configured SystemJS by adding 'xml2js': '../node_modules/xml2js' to map and 'xml2js': { defaultExtension: 'js' } to packages
  • import Parser of xml2js in my component via import {Parser} from 'xml2js';
  • try using it in constructor of component:
    var parser = new Parser(); var xml = "<root>Hello xml2js!</root>" parser.parseString(xml, function (err, result) { console.dir(result); });

这是浏览器控制台在打开包含该组件的网页时告诉我的信息:

This is what browser console tells me when opening a webpage containing the component:

module.js:440未捕获的错误:找不到模块"sax"

module.js:440 Uncaught Error: Cannot find module 'sax'

获取文件:///C:/xxx/source/node_modules/xml2js net :: ERR_FILE_NOT_FOUND

GET file:///C:/xxx/source/node_modules/xml2js net::ERR_FILE_NOT_FOUND

以及

index.html:17错误:错误:XHR错误加载文件:///C:/xxx/source/node_modules/xml2js
在ZoneDelegate.invokeTask(文件:///C:/xxx/source/node_modules/zone.js/dist/zone.js:356:38)
在Zone.runTask(文件:///C:/xxx/source/node_modules/zone.js/dist/zone.js:256:48)
在XMLHttpRequest.ZoneTask.invoke(file:///C:/xxx/source/node_modules/zone.js/dist/zone.js:423:34)
从文件://://C:/xxx/source/frontend/angular/components/mycomponent/mycomponent.js

index.html:17 Error: Error: XHR error loading file:///C:/xxx/source/node_modules/xml2js
at ZoneDelegate.invokeTask (file:///C:/xxx/source/node_modules/zone.js/dist/zone.js:356:38)
at Zone.runTask (file:///C:/xxx/source/node_modules/zone.js/dist/zone.js:256:48)
at XMLHttpRequest.ZoneTask.invoke (file:///C:/xxx/source/node_modules/zone.js/dist/zone.js:423:34)
Error loading file:///C:/xxx/source/node_modules/xml2js as "xml2js" from file:///C:/xxx/source/frontend/angular/components/mycomponent/mycomponent.js

"sax"位于通过npm安装的node_modules/xml2js/node_modules/sax中. 我错过了安装外部库的什么?是否有更好"的方法将外部JS库与Angular 2和TypeScript?

"sax" is located in node_modules/xml2js/node_modules/sax as installed via npm. What did I miss installing the external library? Is there a "better" way to use external JS libraries with Angular 2 & TypeScript?

推荐答案

是的,还有更好的方法.

Yes, there is a better way.

您可以使用System.set('xml2js', System.newModule(require('xml2js')))将本地xml2js模块加载到systemjs中.

You can use System.set('xml2js', System.newModule(require('xml2js'))) to load your local xml2js module in systemjs.

这篇关于在Angular 2中使用JS库xml2js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆