【问题标题】:declare routes in sammy js in typescript在 typescript 的 sammy js 中声明路由
【发布时间】:2013-10-16 04:36:31
【问题描述】:

我想使用确定类型的 sammyjs 文件和 typescript 在我的页面上声明一个路由

声明的 Javascript 看起来像这样 -->

    Sammy(function () {
        this.get('#:foobar', function () {
            //doStuff 
            var baz = this.params.foobar;
        });
        this.get('', function () { this.app.runRoute('get', '#All') });
    }).run();

到目前为止,我有这个。

var app: Sammy.Application = Sammy();
app.get('#:foobar', () => {
    //doStuff 
    var baz = this.params.foobar;
});

显然 params 不在“this”的上下文中,所以我更详细的问题是..这是定义 sammy 路线的正确方法吗?如果是,那么我如何访问婴儿车。

【问题讨论】:

    标签: javascript typescript sammy.js


    【解决方案1】:

    我怀疑您遇到的问题是您使用胖箭头语法覆盖了 Sammy 的范围(这会保留您的词法范围)。

    var app: Sammy.Application = Sammy();
    app.get('#:foobar', function () {
        //doStuff 
        var baz = this.params.foobar;
    });
    

    通过使用“function”而不是“() =>”,您可以避免范围保留并允许 Sammy 照常工作。

    【讨论】:

    【解决方案2】:

    你可以使用带参数的 lambda

    var app: Sammy.Application = Sammy();
    app.get('#:foobar', context => {
        //doStuff 
        var baz = context.params.foobar;
    });

    【讨论】:

    • 这是在 Typescript 中使用 Sammy 的正确解决方案。谢谢。
    猜你喜欢
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    相关资源
    最近更新 更多