使用sammyjs打字稿和systemjs

时间:2016-04-25 22:11:31

标签: javascript systemjs typing sammy.js typescript1.8

我正在尝试使用在Typescript和systemjs中编写的sammyjs作为模块加载器来创建一个简单的路由应用程序。

但是面临加载sammy的问题。

以下是代码段

SystemJs配置

System.config({
  transpiler: "typescript",
  defaultJSExtensions: true,
  map: {
    "knockout": "../bower_components/knockout/dist/knockout",
    "sammy": "../bower_components/sammy/lib/sammy",
    "jquery": "../bower_components/jquery/dist/jquery"
  }
});

路线提供商

import { Route } from "./types";
import * as Sammy from "sammy"

export class RouteProvider {
    sammyApp: Sammy.Application;
    defaultRoute: string;

    constructor() {
  }

  configureRoutes = (routes: Array<Route>) => {        
    this.sammyApp = Sammy(() => {
        routes.map((_route: Route) => {
            this.sammyApp.get('#' + _route.path, (context) => {
                _route.callBack(context);
            });
        })
    });

    this.sammyApp.run('#' + this.defaultRoute);
  }
}

当我初始化 RouteProvider 类并调用 configureRoutes 方法时,我收到以下错误,

未捕获的TypeError:Sammy不是函数

我检查了浏览器中的网络选项卡,并且已加载sammy.js。我也得到了正确的类型定义,没有Typescript编译错误。

1 个答案:

答案 0 :(得分:0)

好的,看起来像这句话

import * as Sammy from "sammy"

实际上将函数转换为对象。通过这样做

Sammy = require("sammy");

它有效,即Sammy作为函数输出。希望这会有所帮助。