简体   繁体   English

如何使用aurelia路由器生成正确的详细信息URL?

[英]How can I generate correct detail url using aurelia router?

One of my application's view-models resides under the routes: 我的应用程序的视图模型之一位于以下路线中:

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

and so on. 等等。 It's a list view-model , so whenever user clicks on one of it's rows, he or she should be directed to: 这是一个list view-model ,因此,每当用户单击其中的一行时,都应将其定向到:

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

Seems simple enough, right? 看起来很简单,对吧? However there's a problem with route generation. 但是,路由生成存在问题。 Namely: if I'm browsing through one of the pages (urls ending with page/:pageNumber/ , then calling router.generate gives me details url that looks like that: #/historical-orders/page/1/details/:orderId which is obviously incorrect. My route config: 即:如果我浏览页面之一(以page/:pageNumber/结尾的URL,则调用router.generate会给我提供如下详细信息的URL: #/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
            }
        ]);
    }
}

Historical orders section: 历史订单部分:

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

And the detail-view route generation looks quite ordinarily: 而且,详细视图路线生成通常看起来很像:

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

So how to make the router generate proper urls? 那么如何使路由器生成正确的URL?

The generated URL is not incorrect. 生成的URL不正确。 You are using a child router, that's why the generated url is #/historical-orders/page/1/details/:orderId . 您正在使用子路由器,这就是为什么生成的URL是#/historical-orders/page/1/details/:orderId

To get an URL, such as #/historical-orders/details/:orderId , you should do this: 要获取URL,例如#/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
            }
        ]);
    }
}

Then 然后

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

EDIT To access the current router 编辑访问当前路由器

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Aurelia路由器中定义状态参数 - How can I define state parameters in Aurelia router 在aurelia中使用路由器时如何设置/读取查询字符串? - How do I set/read a query string when using the router in aurelia? 如何使用Aurelia + .Net Core + IIS Web应用程序获取正确的URL - How to fetch the correct url with Aurelia + .Net Core + IIS web application 如何使用反应路由器 4.2.2 的 withRouter 在 React 中获得正确的当前路径? - How can I get the correct current path in React using react router 4.2.2's withRouter? 如何使用Aurelia的路由器定义和渲染子菜单项 - How to define and render submenu-items, using Aurelia's Router 如何正确使用AJAX + pushState生成URL? - How do I correct generate URL with AJAX + pushState? 如何在Aurelia中使用Promises? - How can I use Promises in Aurelia? Aurelia:在路由器的管道步骤中,如何将变量绑定到该路由器? - Aurelia: During a Router's Pipeline Step, how do I bind a variable to that router? 如何使用Iron Router对URL哈希更改做出反应? - How can I react to URL hash change with Iron Router? 如何使用业力在我的Aurelia应用程序中获得代码覆盖率结果? - How can I get code coverage results in my Aurelia app using karma?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM