本文介绍了如果没有指定subroute,Emberjs将重定向到默认路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果网址匹配 / dashboard / ,我正在尝试将 / dashboard / 重定向到 / dashboard / reach / p>

问题是,如果我点击像/ / dashboard / traffic这样的子路由,我仍然重定向到 / dashboard / reach / demographic / p>

Ember的这种方式是什么?



这是我的代码:

 (function(){
App.Router.map(function(match){
this.resource('dashboard',function ){
this.resource('traffic',function(){
this.route('visits');
this.route('pageviews');
返回此.route('duration');
});
返回this.resource('reach',function(){
this.route('demographics');
this .route('over_time');
return this.route('devices');
});
});
return this.route观众');
});

App.DashboardRoute = Ember.Route.extend({
redirect:function(){
return this.transitionTo(' reach.demographics');
}
});

if(!App.ie()){
App.Router.reopen({
location:'history'
});
}

App.reopen({
rootUrl:'/'
});

})。call(this);


解决方案

在DashboardIndexRoute中实现重定向,而不是DashboardRoute。

  App.DashboardIndexRoute = Ember.Route.extend({
redirect:function(){
return this.transitionTo('reach.demographics');
}
});

我为您创建了一个jsbin示例。尝试:







我还从代码中删除了不必要的退货。


I'm trying to redirect /dashboard/ to /dashboard/reach/demographic if the url hits /dashboard/.

Problem is, I still get redirected to /dashboard/reach/demographic/ if I hit a subroute like **/dashboard/traffic.

What is the Ember way of doing this?

Here is my code:

(function() {
    App.Router.map(function(match) {
      this.resource('dashboard', function() {
        this.resource('traffic', function() {
          this.route('visits');
          this.route('pageviews');
          return this.route('duration');
        });
        return this.resource('reach', function() {
          this.route('demographics');
          this.route('over_time');
          return this.route('devices');
        });
      });
      return this.route('audience');
    });

    App.DashboardRoute = Ember.Route.extend({
      redirect: function() {
        return this.transitionTo('reach.demographics');
      }
    });

    if (!App.ie()) {
      App.Router.reopen({
        location: 'history'
      });
    }

    App.reopen({
      rootUrl: '/'
    });

  }).call(this);
解决方案

Implement redirect in DashboardIndexRoute instead of DashboardRoute.

App.DashboardIndexRoute = Ember.Route.extend({
  redirect: function() {
    return this.transitionTo('reach.demographics');
  }
});

I created a jsbin example for you. Try:

http://jsbin.com/odekus/6#/dashboard/

http://jsbin.com/odekus/6#/dashboard/traffic

I also removed unnecessary return's from the code.

这篇关于如果没有指定subroute,Emberjs将重定向到默认路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 12:53