javascript - How to get route parameter in aurelia custom component - Stack Overflow

I'm creating a custom ponent in aureliajs framework and injecting it a Router instance:@inject(Rou

I'm creating a custom ponent in aureliajs framework and injecting it a Router instance:

@inject(Router)
export class Pagination {
    constructor(router) {
        this.router = router;
    }
}

It will be used with list view-models in order to setup some basic pagination. Therefore I need to read current page number from an active route (that would look like: orders/:pageNum. I'm not sure however how to do it? I mean - I know it should probably be placed in Pagination attached method, but how to get to this :pageNum param?

I'm creating a custom ponent in aureliajs framework and injecting it a Router instance:

@inject(Router)
export class Pagination {
    constructor(router) {
        this.router = router;
    }
}

It will be used with list view-models in order to setup some basic pagination. Therefore I need to read current page number from an active route (that would look like: orders/:pageNum. I'm not sure however how to do it? I mean - I know it should probably be placed in Pagination attached method, but how to get to this :pageNum param?

Share Improve this question asked Aug 10, 2016 at 16:30 Marek M.Marek M. 3,9409 gold badges52 silver badges104 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Create a bindable property in your custom element. Take the page number from the active route and bind it to the custom element. For instance:

import { bindable } from 'aurelia-framework';

@inject(Router)
export class Pagination {

    @bindable page;

    constructor(router) {
        this.router = router;
    }
}

Usage:

<pagination page.bind="page"></pagination>

To get the page number in the active route, use the activate() hook:

activate(params) {
   this.page = params.pageNum;
}

You can use router.currentInstruction.params and router.currentInstruction.queryParams.

You can also observe the router to be notified when the route changes:

let sub = this.bindingEngine.propertyObserver(
    this.router, 'currentInstruction').subscribe(currentInstruction => {

  console.log(currentInstruction);

});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745190055a4615823.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信