ui-router does not run a parent state's resolve promise when using $state.go()

孤者浪人 提交于 2019-12-23 14:35:52

问题


I have an abstract parent state with a resolve promise which returns information about the current user. This information is injected into controllers of all the child states.

However when I use $state.go('parent.child') the resolve promise function is not being executed. If I browse to the URL representing that state though, it executes fine.

Do I need to specify the resolve object on each child state and omit it from the parent?


回答1:


A parent state resolve will only resolve once for a given set of $stateParams values. If the parent state does not rely on $stateParams or does not use any, then its dependencies will only be resolved once, regardless of any child state changes.

The difference in behaviour you are seeing is a child state change will not result in the parent resources being reloaded, whereas the location change will as the full parent and child states are reloaded.

You can observe this behaviour in this plunk.

The example has a $stateParams value in the parent state on which the children are dependent. Changing state via $state.go or ui-sref (both methods are provided) will result in a refresh of the parent resource. However, changing state to a child state without a change to the $stateParams parameter will not refresh the parent resource and the previously returned value will be provided.




回答2:


I think you just have a miss understanding of resolve, only the current state's resolves get called. You can just copy the resolve from the parent to the childs. I am pretty sure there is never a reason to even have a resolve on an abstract state. The abstract state will give you $stateParams, thats it though.



来源:https://stackoverflow.com/questions/25354046/ui-router-does-not-run-a-parent-states-resolve-promise-when-using-state-go

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!