如何使用打字稿将sax解析器导入angular2 [英] how to import sax parser into angular2 using typescript

查看:139
本文介绍了如何使用打字稿将sax解析器导入angular2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图包含sax解析器并遇到困难[parser.ts].

Trying to include the sax parser and running into difficulties [parser.ts].

import sax = require("sax");
export class MyParser {
   //my parser code 
}

当我尝试使用它[app.component.ts]:

When I try to use it [app.component.ts]:

import {MyParser} from './parser'
...
constructor(private http: Http, private parser: MyParser) {}

我收到此错误

Evaluating http://localhost:3000/sax

我知道这意味着SystemJS无法找到该模块. Sax安装在node_modules中,并列在package.json中.我已经使用

Which I know means SystemJS cannot find the module. Sax is installed in node_modules and listed in package.json. I've installed typings using

typings install --ambient sax

但是即使我的tsconfig.json被排除在外,也会收到很多重复的标识符警告

But getting lots of duplicate identifier warnings even though my tsconfig.json is excluding

"exclude": [
  "node_modules",
  "typings/main",
  "typings/main.d.ts"
]

最后,isaacs在他的示例中使用了这样的代码

Lastly, isaacs uses code like this in his examples

 tag.parent
 tag.children

键入(d.ts)不支持此操作.

This isn't supported by the typing (d.ts).

有人可以同时安装带有TS和ng2的sax吗?

Does anyone have a working install of sax with TS and ng2?

推荐答案

您所遇到的错误是在运行时.因此,我认为您可以尝试通过以下方式配置SystemJS:

The error you have is at runtime. So I think that you could try to configure SystemJS this way:

<script>
  System.config({
    paths: {
      sax: './node_modules/sax/lib/sax.js'
    }
  })
</script>

这样,您将可以导入Sax:

This way you will be able to import Sax:

import {SAXParser} from 'sax';

这篇关于如何使用打字稿将sax解析器导入angular2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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