如何使用aurelia路由器生成正确的详细UR​​L?

时间:2016-08-14 15:36:52

标签: javascript aurelia aurelia-router

我的应用程序的一个视图模型位于路径下:

http://localhost:9000/#/historical-orders/
http://localhost:9000/#/historical-orders/page/2/
http://localhost:9000/#/historical-orders/page/3/

等等。它是list view-model,因此每当用户点击其中一行时,他或她应该被定向到:

http://localhost:9000/#/historical-orders/details/:orderId

看起来很简单吧?然而,路线生成存在问题。即:如果我正在浏览其中一个页面(以page/:pageNumber/结尾的网址,则调用router.generate会向我提供看起来像这样的详细信息网址:#/historical-orders/page/1/details/:orderId这显然是不正确的。我的路线配置:

export class App {
    configureRouter(config, route) {
        config.map([
            {
                route: ["historical-orders", "historical-orders/page/:pageNumber"],
                moduleId: "orders/historical-orders-section",
                name: "historical-orders-section",
                title: "Historical orders",
                nav: true
            }
        ]);
    }
}

历史订单部分:

export class HistoricalOrders {
    configureRouter(config, router) {
        config.map([
            { 
                route: "", 
                moduleId: "orders/historical/orders-list", 
                title: "Orders history", 
                nav: false
            }, 
            {
                route: "details/:id",
                moduleId: "orders/historical/order-details",
                name: "historical-orders-details",
                title: "Details",
                nav: false
            },
        ]);
    }
}

细节视图路径生成看起来很普遍:

this._router.generate("historical-orders-details", { id: order.pk });

那么如何让路由器生成合适的URL呢?

1 个答案:

答案 0 :(得分:0)

生成的网址不正确。您正在使用子路由器,这就是生成的URL为#/historical-orders/page/1/details/:orderId的原因。

要获取网址,例如#/historical-orders/details/:orderId,您应该这样做:

export class App {
    configureRouter(config, route) {
        config.map([
            {
                route: ["historical-orders", "historical-orders/page/:pageNumber"],
                moduleId: "orders/historical-orders-section",
                name: "historical-orders-section",
                title: "Historical orders",
                nav: true
            },
            {
                route: "orders/historical-orders/details/:id",
                moduleId: "orders/historical/order-details",
                name: "historical-orders-details",
                title: "Details",
                nav: false
            }
        ]);
    }
}

然后

let url = this._router.generate("historical-orders-details", { id: order.pk });

编辑访问当前路由器

import {inject} from 'aurelia-dependency-injection';
import {Router} from 'aurelia-router';

@inject(Router)
export class HistoricalOrders {

    constructor(router) {
       this.router = router; //this is the same app.js router
    }
}