使用骨干路由器查看更改不断恢复到主页

View changes with Backbone router constantly revert to home page

本文关键字:恢复 主页 路由器      更新时间:2023-09-26

我似乎无法解决这个非常简单的问题。

我有一个骨干/马里昂内特应用程序,我将路由器定义为:

app.utils.AppRouter = Backbone.Marionette.AppRouter.extend({
initialize: function(){
    this.route("", "home", function(){
        console.log("In home!");
        app.layout.content.show(new app.views.BrowseRestaurantsLayout());
    });
    this.route("account", "account", function(){
        console.log("In account!");
        app.layout.content.show(new app.views.AccountView());
    });
}
});

在我的代码中的其他地方,我需要导航到#account页面,所以我调用:

 app.router.navigate('account', {trigger:true});

我可以看到URL更改为#account,并且我的AccountView页面确实出现了一瞬间,然后消失,被主页取代。

当我触发更改时,控制台显示:

In account! 
In home! 

我错过了什么?

您的主路由可能是在您的帐户路由之后调用的包罗万象的路由。

当您将路线更改为此路线时会发生什么?

this.route("/", "home", function(){
    console.log("In home!");
    app.layout.content.show(new app.views.BrowseRestaurantsLayout());
});

或者也许是路线的顺序。尝试最后添加家用路由器。

你检查过你的模板吗?我遇到了类似的问题,并发现问题是我链接中的 href。

  <a href=#"></a>

它导致路由器在暂时将 URL 切换到我实际想要的 URL 后将我发送回根目录。

我不确定这是否能解决任何问题,但是您配置路由器的方式很奇怪。我认为不需要Marionette的appRouter,因为您没有使用它的功能,并且我不会在初始化方法中配置路由。以这种方式配置路由器会更"像骨干网":


app.utils.AppRouter = Backbone.Router.extend({
  routes: {
    "": "home",
    "account": "account"
  },
  home: function(){
    console.log("In home!");
    app.layout.content.show(new app.views.BrowseRestaurantsLayout());
  },
  account: function(){
    console.log("In account!");
    app.layout.content.show(new app.views.AccountView());
  }
});

您定义路由的方式可能与此有关。