抽象的观点相互矛盾的UI,路由器的路由路由、路由器、抽象、矛盾

2023-09-13 03:18:26 作者:╭⌒云卷云舒⌒╮

我使用的用户界面 - 路由器,需要在其他视图子视图。我需要呈现狗视图中的主人的看法。为此目的,以下工作:

I am using UI-Router and need a subview in another view. I need to render the "owner" view within the "dog" view. The following works for that purpose:

UI-路由器的配置如下:

UI-Router config is as follows

.state('dogAbstract', {
    url: '/dog/:dogId',
    abstract: true,
    templateUrl: 'client/dogs/views/dog.ng.html',
    controller: 'dogCtrl',
})
.state('dog', {
    url: "",
    parent: "dogAbstract",
    views: {
        "owner": {
            templateUrl: 'client/owners/views/owner.ng.html',
            controller: 'ownerCtrl'
        }
    }
})
.state('dogsList', {
    url: '/dogs',
    templateUrl: 'client/moves/views/dogs-list.ng.html',
    controller: 'dogsListCtrl',
})

的问题是,链接结构是最理想的。目前访问特定的狗,你必须去 /狗/ dogId 。不过,我想它是 /狗/ dogId ,因为去与标准的REST原则更好。不幸的是,当我改变'dogAbstract网址 /狗,它与dogsList页面冲突,我只能有一个或其他。

The problem is that the url structure is suboptimal. Currently to access a specific "dog" you have to go to /dog/dogId. However, I would like it to be /dogs/dogId because that goes better with standard REST principles. Unfortunately, when I change the 'dogAbstract' url to /dogs, it conflicts with the "dogsList" page, and I can only have one or the other.

有没有一种方法,使其工作,这样我可以在列表/狗并查看个人狗 /狗/ dogId

Is there a way to make it work so that I can have the list at /dogs and view an individual dog at /dogs/dogId?

推荐答案

有一个工作plunker

问题的关键是要改变定义的顺序:

The point is to change the order of definition:

少特定的URL第一,更详细的最后一个

the less specific url first, the more detailed last

由于UI的路由器使用状态声明的顺序来设置优先级。首先它匹配 - 使用:

Because UI-Router uses the order of state declaration to set the priority. First which matches - is used:

// if /dogs is url, this will match
.state('dogsList', {
  url: '/dogs',
  ...
})

// if /dogs + something e.g. /dogs/1 - this ONLY will fit
.state('dogAbstract', {
  url: '/dogs/:dogId',
  ...
})

检查它在行动这里

 
精彩推荐
图片推荐