Angular ui-router 中 $state.transitionTo() 和 $state.go() 之间的区别区别、router、ui、Angular

2023-09-06 08:24:53 作者:烈酒烫喉

在 AngularJS 中,我看到有时我们使用 $state.transitionTo(),有时我们使用 $state.go().谁能告诉我它们有何不同以及何时应该使用一个而不是另一个?

In AngularJS, I see sometimes we use $state.transitionTo() and sometimes we use $state.go(). Can anyone tell me how they differ and when one should be used over the other?

推荐答案

你指的是AngularUI 路由器?如果是这样,wiki 指定差异:

Are you referring to the AngularUI Router? If so, the wiki specifies the differences:

$state.go(to [, toParams] [, options])

返回一个表示转换状态的 Promise.

Returns a Promise representing the state of the transition.

转换到新状态的便捷方法.$state.go 在内部调用 $state.transitionTo 但自动将选项设置为 { location: true, inherit: true, relative: $state.$current, notify:真}.这使您可以轻松地使用绝对路径或相对路径,并仅指定您想要更新的参数(同时让未指定的参数从当前状态继承).

Convenience method for transitioning to a new state. $state.go calls $state.transitionTo internally but automatically sets options to { location: true, inherit: true, relative: $state.$current, notify: true }. This allows you to easily use an absolute or relative to path and specify only the parameters you'd like to update (while letting unspecified parameters inherit from the current state).

$state.transitionTo(to, toParams [, options])

返回一个表示转换状态的 Promise.

Returns a Promise representing the state of the transition.

转换到新状态的低级方法.$state.go() 在内部使用 transitionTo.$state.go() 在大多数情况下推荐使用.

Low-level method for transitioning to a new state. $state.go() uses transitionTo internally. $state.go() is recommended in most situations.