简体   繁体   English

如何使用打字稿将 sax 解析器导入 angular2

[英]how to import sax parser into angular2 using typescript

Trying to include the sax parser and running into difficulties [parser.ts].试图包含 sax 解析器并遇到困难 [parser.ts]。

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

When I try to use it [app.component.ts]:当我尝试使用它 [app.component.ts] 时:

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

I get this error我收到这个错误

Evaluating http://localhost:3000/sax

Which I know means SystemJS cannot find the module.我知道这意味着 SystemJS 找不到模块。 Sax is installed in node_modules and listed in package.json. Sax 安装在 node_modules 中并列在 package.json 中。 I've installed typings using我已经安装了打字使用

typings install --ambient sax

But getting lots of duplicate identifier warnings even though my tsconfig.json is excluding但是即使我的 tsconfig.json 不包括在内,也会收到很多重复的标识符警告

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

Lastly, isaacs uses code like this in his examples最后,isaacs 在他的例子中使用了这样的代码

 tag.parent
 tag.children

This isn't supported by the typing (d.ts).这不受打字 (d.ts) 支持。

Does anyone have a working install of sax with TS and ng2?有没有人用 TS 和 ng2 安装了 sax?

The error you have is at runtime.您遇到的错误是在运行时。 So I think that you could try to configure SystemJS this way:所以我认为你可以尝试这样配置 SystemJS:

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

This way you will be able to import Sax:这样您就可以导入 Sax:

import {SAXParser} from 'sax';

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

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