Angular ui-router:你能在不改变 URL 的情况下改变状态吗? [英] Angular ui-router: Can you change state without changing 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,但有时不会.我觉得状态的概念应该与路由分开/可选.
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).
这篇关于Angular ui-router:你能在不改变 URL 的情况下改变状态吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!