angularjs - Angular ui-router 1.0.0rc 过渡在重定向上被取代

标签 angularjs redirect angular-ui-router

Angular 1.6.1 和 ui-router 1.0.0rc;

我在从一种状态重定向到另一种状态时遇到了 ui-router 问题。

TransitionRejection 已被注册,因为它已被取代。

Transition #1 r0: Started  -> "Transition#1( 'intro'{} -> 'foo'{} )"
Transition #1 r0: <- Rejected "Transition#1( 'intro'{} -> 'foo'{} )", reason: TransitionRejection(type: 2, message: The transition has been superseded by a different transition, detail: 'bar'{"id":"uuid-1234-uuid"})
Transition #2 r0: Ignored  <> "Transition#2( 'intro'{} -> 'intro'{} )"
Transition #3 r0: Started  -> "Transition#3( 'intro'{} -> 'bar'{"id":"uuid-1234-uuid"} )"
Transition #3 r0: <- Success  "Transition#3( 'intro'{} -> 'bar'{"id":"uuid-1234-uuid"} )", final state: bar

注意:全局有一个包罗万象的内容,当用户在加载状态时未解析时,它用于身份验证控制:

// state 'intro' is '/'
.config($urlRouterProvider => {
  $urlRouterProvider.otherwise('/'); 
})
.run(($transitions, $trace) => {
  $trace.enable('TRANSITION');
  $transitions.onError({}, trans => trans.router.stateService.go('intro'));
}

过渡片段

// there is no foo component, it's a straight redirect
.component('bar', {
  template: someTemplate,
  controller: someCtrl
})
.config($stateProvider => {
  let states = [
    {
      name: 'foo',
      url: '/foo',
      redirectTo: { state: 'bar', params: { id: 'uuid-1234-uuid' } }
    },
    {
      name: 'bar',
      url: '/bar/:id',
      component: 'bar'
    }
  ];

  states.forEach(s => $stateProvider.state(s));
})

虽然在这个 intro -> foo 示例中,最终它最终处于正确的状态 bar,但其他转换(当不从 intro(默认)开始时) bar,但从 baz, qux, ..bar)最终会陷入无限的转换循环。

例如,从 quxfoo,它应该重定向到 bar,但陷入了 qux -> intro 循环:(我没有之前的 qux -> foo 错误消息,因为浏览器崩溃了)

TransitionRejection(type: 2, message: The transition has been superseded by a different transition, detail: Transition#2704( 'qux'{} -> 'intro'{} ))

有谁有重定向的解决方案,或者可以看看我是否正确使用了全局包罗万象? Catch all 会导致死亡循环,但这对于首先触发它的重定向拒绝问题来说是次要的。

最佳答案

当您尝试在上一个状态更改事件完成之前触发状态更改时,会发生转换被取代错误。 解决这个问题的一个简单方法是在超时后调用状态更改。允许当前状态变化完成,然后触发下一个状态变化。

 $timeout(function(){
   $state.go('statename')
})

注意:这里的超时没有定义时间,只是表示当前进程全部完成后才会执行

关于angularjs - Angular ui-router 1.0.0rc 过渡在重定向上被取代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42659302/

相关文章:

angularjs - 使用 AngularJS 处理 HTTP POST 请求

angularjs - Dygraphs 不适用于 ng-repeat

php - 我应该将移动检测放在 .htaccess 或 php 的什么位置?

javascript - 在执行配置之前等待异步调用运行(Angular JS)

javascript - 使 AngularJS 路由命名组非贪婪

javascript - Angular : How to handle two-part directive

ruby-on-rails - 为什么 `log/test.log`在 `http://www.example.com/`消息中显示 `Redirected to`?

.htaccess - HTACCESS 重写在 Ubuntu 14、Apache2 上不起作用

javascript - Angular UI 路由器 : decide child state template on the basis of parent resolved object

javascript - html partials 中的样式标签