declare routes in sammy js in typescript

大兔子大兔子 提交于 2019-12-10 20:08:00

问题


I would like to use the definitely typed sammyjs file in conjunction with typescript to declare a route on my page

Javascript for the declaration would look like this -->

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

So far I have this.

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

Obviously params is not in the context of 'this' so my question in more detail is.. Is this the correct way to define the sammy routes and if yes then how do I access the prams.


回答1:


I suspect the problem you have is that you are overriding Sammy's scope by using the fat-arrow syntax (which preserves your lexical scope).

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

By using "function" instead of "() =>" you avoid scope preservation and allow Sammy to work as usual.




回答2:


You can use lambda with parameter

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


来源:https://stackoverflow.com/questions/19395335/declare-routes-in-sammy-js-in-typescript

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