每次查看页面时如何执行代码

pad*_*bro 11 sapui5

我每次查看页面时都会搜索模式以执行代码(在我的情况下,检索数据以便从服务器可视化)(每个tipe页面都由splitApp.toDetail或splitApp.backDetail调用).我该怎么做??onBeforeRendering和onAfterRendering仅在第一次执行..

Hao*_*jie 13

有一个解决方案给你.每次触发导航时都会发生一个名为routeMatched的事件.您可以在详细信息页面中附加事件.

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

handleRouteMatched : function (evt) {
    //Check whether is the detail page is matched.
    if (evt.getParameter("name") !== "detail") {
        return;
    //You code here to run every time when your detail page is called.
}
Run Code Online (Sandbox Code Playgroud)

问候,艾伦

  • 很好的答案,但只有在使用路由时才有可能. (2认同)

Tim*_*ach 7

onBeforeShow在目标视图中使用它.

onBeforeShow : function(evt) {
    // gets called everytime the user 
    // navigates to this view
},
Run Code Online (Sandbox Code Playgroud)

这是触发功能一个NavContainer在导航的情况下,它的孩子.它记录在NavContainerChild中.

  • 我注册到onInit ...它工作...`// INIT onInit:function(){//注册beforeShow事件this.getView().addEventDelegate({//没有将控制器添加为委托以避免控制器功能与onBeforeShow上的事件类似的名称:jQuery.proxy(function(evt){this.onBeforeShow(evt);},this)}); },onBeforeShow:function(evt){alert("onBeforeShow"); }` (2认同)

dan*_*pop 5

如果使用路由,另一个版本的Allen的代码:

 onInit : function () {
   this._oRouter = sap.ui.core.UIComponent.getRouterFor(this);
   this._oRouter.getRoute("detail").attachMatched(this.handleRouteMatched, this);
},

  handleRouteMatched : function (evt) {
    //You code here to run every time when your detail page is called.
  }
Run Code Online (Sandbox Code Playgroud)