在打字稿中的sammy js中声明路由 [英] declare routes in sammy js in typescript

查看:57
本文介绍了在打字稿中的sammy js中声明路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想结合使用绝对类型化的sammyjs文件和typescript在我的页面上声明路由

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

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

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;
});

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

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.

推荐答案

我怀疑您遇到的问题是,您正在使用fat-arrow语法(保留词汇范围)来覆盖Sammy的范围.

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;
});

通过使用函数"而不是()=>",可以避免范围保留,并允许Sammy像往常一样工作.

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

这篇关于在打字稿中的sammy js中声明路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆