问题描述
我正在尝试在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>
通过添加 - 配置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);});
'xml2js': '../node_modules/xml2js'
到映射和'xml2js': { defaultExtension: 'js' }
到包- 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:
和
以及
"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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!