ui-router does not run a parent state's resolv

2019-06-28 01:16发布

问题:

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.