Declare routes in sammy js in typescript

I would like to use a specifically typed sammyjs file in combination with typescript to declare a route on my page

Javascript for the ad will look like this:>

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

While I have it.

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

Obviously, the parameters are not in the context of 'this', so my question is more detailed: .. This is the correct way to determine sammy routes, and if so, how can I access the wheelchairs.

+3
source share
2 answers

I suspect the problem is that you are redefining the Sammy area using the thick arrow syntax (which preserves the lexical scope).

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

Using a "function" instead of "() =>", you avoid saving the scope and letting Sammy work as usual.

+4
source

You can use lambda with parameter

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

All Articles