在打字稿中parsingXML

使用Typescript 2.0(tsc版本2.0.3)。

我没有成功尝试在angular2 / typescript应用程序中使用xml2js节点库(请参阅https://www.npmjs.com/package/xml2js )。 似乎很清楚,我不熟悉SystemJS设置库使用的方式。

要安装xml2js库,我做了以下操作:

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

这里是相关的文件:

tsconfig.json:

 { "compilerOptions": { "target": "es5", "module": "commonjs", "moduleResolution": "node", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false, "noImplicitAny": false, "outDir" : "build" } } 

systemjs.config.js

 /** * System configuration for Angular samples * Adjust as necessary for your application needs. */ (function (global) { System.config({ paths: { // paths serve as alias 'npm:': 'node_modules/' }, // map tells the System loader where to look for things map: { // our app is within the app folder app: 'build', // angular bundles '@angular/core': 'npm:@angular/core/bundles/core.umd.js', '@angular/common': 'npm:@angular/common/bundles/common.umd.js', '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js', '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js', '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js', '@angular/http': 'npm:@angular/http/bundles/http.umd.js', '@angular/router': 'npm:@angular/router/bundles/router.umd.js', '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js', '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js', // other libraries 'rxjs': 'npm:rxjs', 'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js', 'lodash' : 'npm:lodash', 'xml2js' : 'npm:xml2js' }, // packages tells the System loader how to load when no filename and/or no extension packages: { app: { main: './main.js', defaultExtension: 'js' }, rxjs: { defaultExtension: 'js' }, lodash: { main : 'index.js', defaultExtension: 'js' }, xml2js: { defaultExtension: 'js' } } }); })(this); 

消耗文件:

 import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; import 'rxjs/Rx'; import * as _ from 'lodash'; import * as xml2js from 'xml2js'; @Injectable() export class CatalogService { constructor (private http:Http) { } getCatalog() { //return Promise.resolve(['This', 'That']); var xml = "<root>Hello xml2js!</root>" xml2js.parseString(xml, function (err, result) { alert(result); }); alert(_.indexOf([1, 2, 1, 2], 2)); } } 

index.html文件

 <html> <head> <title>Angular QuickStart</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="styles.css"> <!-- 1. Load libraries --> <!-- Polyfill for older browsers --> <script src="node_modules/core-js/client/shim.min.js"></script> <script src="node_modules/zone.js/dist/zone.js"></script> <script src="node_modules/reflect-metadata/Reflect.js"></script> <script src="node_modules/systemjs/dist/system.src.js"></script> <!-- 2. Configure SystemJS --> <script src="systemjs.config.js"></script> <script> System.import('app').catch(function(err){ console.error(err); }); </script> </head> <!-- 3. Display the application --> <body> <dsa-app>Loading DSA App...</dsa-app> </body> </html> 

用这个设置,我得到一个错误如下:

zone.js:1382 GET http:// localhost:3000 / node_modules / xml2js / 404(Not Found)

lodash库加载好,所以我很确定这里的问题与xml2js库的定义方式比我的设置更多。

我见过的最接近的解决scheme是有点过时的Angular 2.0,但我把它放在这里作为参考: 使用JS库xml2js Angular 2

虽然这个问题具体解决了使用这个库的问题,但是关于如何将xmlparsing/转换成TypeScript中可pipe理的东西的任何其他想法也将派上用场。


更新

正如所build议我开始添加“主要”定义到我的systemjsconfiguration。 然而,我认为这个依赖的一部分是从节点内容中加载/计算出来的。

因此修改system.config.js有:

  xml2js: { main: 'lib/xml2js.js', defaultExtension: 'js' }, 

将问题移到现在我得到了sax和xmlbuilder的404条目。

因此,我也添加了以下内容:

  sax: { main: 'lib/sax.js', defaultExtension: 'js' }, xmlbuilder: { main: 'lib/index.js', defaultExtension: 'js' } 

其中的地址是sax和xmlbuilder,但是会popup更多的错误。

我认为使用SystemJS和tsconfig的整个想法是,这些将从节点模块中找出来,在包上添加所有的树依赖关系似乎失败了目的。

我认为这里的问题是在你的systemjsconfiguration – 你没有定义main的xml2js。 由于系统js不知道要加载哪个文件,因此试图直接加载NPM目录(加载/ node_modules / xml2js作为一个文件,显然不会工作)。 请注意,您的lodashconfiguration确实指定了'index.js',据推测导致/node_modules/lodash/index.js在您导入lodash时被加载,这就是为什么这是有效的。

如果你在你的systemjsconfiguration中为xml2js指定了一个指向lib/xml2js.js的'main',那么systemjs应该能够正确地find它。

正如@ tim-perry所指出的那样,这里的问题是软件包的定义方式。 在我的工作中还有一个问题,就是我对SystemJS会为我做什么做出错误的假设。 我需要查看一个不同的工具(从JSPM开始),在加载基本模块时执行树遍历。 在我的例子中,lodash正在工作,因为它是自包含的。 然而,xml2js有很多的依赖关系,有很多的依赖关系,有很多的依赖关系…因此,应用为每个依赖关系添加包定义的解决scheme照顾的问题,虽然在一个非常繁琐的方式。

再一次,感谢@ tim-perry和@artem帮助了这一点。