未捕获的TypeError:this.getRouter不是函数

时间:2016-09-27 06:02:04

标签: sapui5 sap-fiori

要将object路由添加到主路由(表页​​面),但返回this.getRouter is not a function错误。

在主控制器中:

    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}

如何解决此错误?

2 个答案:

答案 0 :(得分:2)

尝试

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) },

答案 1 :(得分:1)

尝试

return this.getOwnerComponent().getRouter();

而不是你现在在getRouter函数中拥有的东西。