Uncaught TypeError:this.getRouter is not a function

Tina Chen picture Tina Chen · Sep 27, 2016 · Viewed 9.6k times · Source

Tring to add an object route to the main route(table page), but return this.getRouter is not a function error.

In main Controller:

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

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

In Component.js(I've checked , it's already loaded, no error produced)

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();
        }

    });
}

);

this value in _showObject:

f {mEventRegistry: Object, oView: f}

How to fix this error?

Answer

Tina Chen picture Tina Chen · Sep 27, 2016

Tried

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

And it's working.

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


Thanks to @hdereli

I forgot to include the helper : BaseController.js

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