【问题标题】:how to import sax parser into angular2 using typescript如何使用打字稿将 sax 解析器导入 angular2
【发布时间】:2016-03-13 14:38:37
【问题描述】:

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

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

当我尝试使用它时[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 中。我已经安装了打字使用

typings install --ambient sax

但即使我的 tsconfig.json 不包括在内,也会收到大量重复标识符警告

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

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

 tag.parent
 tag.children

键入 (d.ts) 不支持此功能。

有人用 TS 和 ng2 安装 sax 吗?

【问题讨论】:

  • 代码看起来不错,可能是你配置SystemJS的方式有问题?

标签: json typescript angular sax


【解决方案1】:

您遇到的错误是在运行时。所以我认为你可以尝试这样配置 SystemJS:

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

这样你就可以导入萨克斯了:

import {SAXParser} from 'sax';

【讨论】:

    猜你喜欢
    • 2016-10-27
    • 2011-06-17
    • 2016-05-17
    • 2020-06-14
    • 2018-04-26
    • 2017-03-31
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多