how to import sax parser into angular2 using typescript

核能气质少年 提交于 2019-12-11 09:55:44

问题


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

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

When I try to use it [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. Sax is installed in node_modules and listed in 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

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

Lastly, isaacs uses code like this in his examples

 tag.parent
 tag.children

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

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


回答1:


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>

This way you will be able to import Sax:

import {SAXParser} from 'sax';


来源:https://stackoverflow.com/questions/35971560/how-to-import-sax-parser-into-angular2-using-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!