2016-09-27 50 views
1

將主路由(表頁​​)添加object路由,但返回this.getRouter is not a function錯誤。Uncaught TypeError:this.getRouter不是函數

在主控制器:

onPress : function(oEvent) { 
     this._showObject(oEvent.getSource()); 
    }, 

    _showObject : function (oItem) { 
     this.getRouter().navTo("object", { 
      objectId: oItem.getBindingContext().getProperty("task_id") 
     }); 
    }, 

在Component.js(我檢查了,它已經加載,沒有產生錯誤)

sap.ui.define(["sap/ui/core/UIComponent"], 
function (UIComponent) { 
    "use strict"; 

    return UIComponent.extend("cts.mobile.Component", { 
     metadata : { 
      rootView : "cts.mobile.view.TaskTest", 
      routing : { 
       "config": { 
        "routerClass": "sap.m.routing.Router", 
        "viewType": "XML", 
        "viewPath": "cts.mobile.view", 
        "controlId": "taskapp", //in task.view.xml 
        "controlAggregation": "pages", 
        "async": true 
       }, 
       "routes": [ 
        { 
         "pattern": "", 
         "name": "task", 
         "target": "task" 
        }, 
        { 
         "pattern": "ProductCollection/{objectId}", 
         "name": "object", 
         "target": "object" 
        } 
       ], 
       "targets": { 
        "worklist": { 
         "viewName": "TaskTest", 
         "viewId": "TaskTest", 
         "viewLevel": 1 
        }, 
        "object": { 
         "viewName": "Object", 
         "viewId": "object", 
         "viewLevel": 2 
        } 
       } 
      } 
     }, 

     init : function() { 
      UIComponent.prototype.init.apply(this, arguments); 

      // Parse the current url and display the targets of the route that matches the hash 
      this.getRouter().initialize(); 
     } 

    }); 
} 

);

這個值_showObject:

f {mEventRegistry: Object, oView: f}

如何解決這個問題?

+1

您可以定義一個BaseController類並使用它來定義其他控制器。 SAPUI5示例具有此基礎控制器。並且在那裏定義getRouter方法。你錯過了那部分。 – Huseyin

回答

1

嘗試

​​

而不是你現在擁有的getRouter函數。

0

試圖

onPress : function(oEvent) { 
    var oRouter = sap.ui.core.UIComponent.getRouterFor(this); 
    oRouter.navTo("object", { 
    objectId: 
    oEvent.getSource().getBindingContext().getProperty("task_id") 
}); 

,它的工作。

裁判:https://openui5.hana.ondemand.com/#docs/guide/e5200ee755f344c8aef8efcbab3308fb.html


由於@hdereli

我忘了,包括助手:BaseController.js

getRouter : function() { return sap.ui.core.UIComponent.getRouterFor(this) },

相關問題