Angular ui-router:您可以在不更改 URL 的情况下更改状态吗?

ui-router 的 多个嵌套视图 功能非常好 - 您可以轻松地从应用的一个 状态 跳转到另一个.

The multiple nested views functionality of the ui-router is very nice - you can easily jump from one state of your app to another.

有时您可能想更改 URL,但有时不想.我觉得 state 的概念应该与 routing 分开/可选.

Sometimes you might want to change the URL, but sometimes not. I feel like the concept of state should be separate/optional from routing.

这是一个 plunker,说明了我的意思. 这是 ui-router 文档,有 2 处小改动如下:

Here's a plunker that shows what I mean. This is a fork of one of the plunkers in the ui-router documentation, with 2 minor changes noted below:

.state('route1', {
        url: "/route", // <---- URL IS SHARED WITH ROUTE2
        views: {
            "viewA": {
                template: "route1.viewA"
            },
            "viewB": {
                template: "route1.viewB"
            }
        }
    })
    .state('route2', {
        url: "/route", // <---- URL IS SHARED WITH ROUTE1
        views: {
            "viewA": {
                template: "route2.viewA"
            },
            "viewB": {
                template: "route2.viewB"
            }
        }
    })

这似乎有效 - URL 保持不变.同样,这里做了多少冗余工作?这是经过批准/测试的用法吗?

This seems to work - the URL stays the same. Again, how much redundant work is done here? Is this an approved/tested usage?

如果您可以从状态中省略 url 那就太好了..

It would be nice if you could omit the url from a state..

更新问题:这是经过批准/测试的用法吗?

Update question: Is this an approved/tested usage?

推荐答案

你绝对可以拥有一个没有 URL 的状态.事实上,您所在的州都不需要 URL.这是设计的核心部分.话虽如此,我不会做你上面做的.

You can absolutely have a state without a URL. In fact, none of your states need URLs. That's a core part of the design. Having said that, I wouldn't do what you did above.

如果您希望两个状态具有相同的 URL,请创建一个 抽象父状态,为其分配一个 URL,并使其两个状态成为它的子状态(任何一个都没有 URL).

If you want two states to have the same URL, create an abstract parent state, assign a URL to it, and make the two states children of it (with no URL for either one).

相关文章