2017-03-01 39 views
-1

我有兩個意見:登錄和菜單。如何在每次訪問視圖時調用onInit()?

場景:

登錄 - >onInit() , onBeforeRendering() , onAfterRendering()被稱爲Menu.controller.js 並轉到菜單視圖。

註銷 - >deleteData(); oRouter.navTo("Login", {}, true);返回 登錄視圖。

登錄 - >onInit() , onBeforeRendering() , onAfterRendering() 不會被調用並進入菜單視圖。

我試着我的菜單控制器連接到OnInit的具體路線,但它不工作:

function onInit(){ 
    this._oRouter = sap.ui.core.UIComponent.getRouterFor(this); 
    this._oRouter.attachRouteMatched(initMenu, this); 
    this.initMenu(); 
} 

如何強制再次調用上面的方法?我需要重新加載onInit()函數中的方法initMenu()才能正確接收菜單。

回答

2

onInit只被調用一次。其Sapui5的生命週期方法。如果你想要一個函數,每次瀏覽時都會調用,然後使用onAfterRendering或onBeforeRendering。其他方面從路由匹配創建函數並調用該函數。

上面的路線匹配應該是這樣的。

this._oRouter = sap.ui.core.UIComponent.getRouterFor(this); 
this._oRouter.attachRouteMatched(this.yurfunction, this); 

//現在做如下

yurfunction(){ //now give call to you function from here } 
+0

我已經嘗試了兩件事,但他們不工作。如果我從路由匹配中調用該函數,它在註銷時也會運行。如果我使用onAfterRendering或onBeforeRendering,則不會發生任何事情,因爲頁面不會重新渲染。 –

+0

那麼這只是路由匹配時出現的問題,創建一個全局變量,並且一旦執行函數就將其賦值爲真使該全局變量爲假。 sap.ui.getCore()。urVariable = true;在onInit中執行此操作。現在,如果(sap.ui.getCore()。urVariable){//你的stuff.sap.ui.getCore()。urVariable = false; }將其設置爲false。 –

相關問題