簡體   English   中英

使用Backbone路由器查看更改會不斷恢復到主頁

[英]View changes with Backbone router constantly revert to home page

我似乎無法解決這個簡單易懂的問題。

我有一個Backbone / Marionnette應用程序,我將路由器定義為:

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后,它導致路由器將我發送回root。

我不確定這是否會解決任何問題,但配置路由器的方式很奇怪。 我沒有看到需要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());
  }
});

你定義路線的方式可能與它有關。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM