SAPUI5-MyRouter.js工作不正常

SAPUI5-MyRouter.js工作不正常,sapui5,Sapui5,我遵循SAPUI5的MVC模型。我有一个MyRouter.js,其中包含以下代码: jQuery.sap.declare("sap.ui.demo.Onepage.MyRouter"); sap.ui.demo.Onepage.MyRouter={ /** * to monkey patch the router with the mobile nav back handling */ myNavBack : function (route, data)

我遵循SAPUI5的MVC模型。我有一个MyRouter.js,其中包含以下代码:

jQuery.sap.declare("sap.ui.demo.Onepage.MyRouter");

sap.ui.demo.Onepage.MyRouter={

    /**
     * to monkey patch the router with the mobile nav back handling
     */
    myNavBack : function (route, data) {
        var history = sap.ui.core.routing.History.getInstance();
        var url = this.getURL(route, data);
        var direction = history.getDirection(url);
        if ("Backwards" === direction) {
            window.history.go(-1);
        } else {
            var replace = true; // otherwise we go backwards with a forward history
            this.navTo(route, data, replace);
        }
    },

    /**
     * to monkey patch the router with a nav to method that
     * does not write hashes but load the views properly
     */
    myNavToWithoutHash : function (viewName, viewType, master, data) {
        var oapp = sap.ui.getCore().byId("Myapp");
        var oview = this.getView(viewName, viewType);
        oapp.addPage(oview, master);
        oapp.to(view.getId(), "show", data);
    }
};
Component.js:

jQuery.sap.declare("sap.ui.demo.Onepage.Component");

sap.ui.core.UIComponent.extend("sap.ui.demo.Onepage.Component", {

    metadata : {
        routing : {
            config : {
                viewType : "XML",
                viewPath : "view",
                targetControl : "Myapp",
                targetAggregation: "pages",
                clearTarget : false,
                transition: "slide"
            },
                        routes : [
                {
                    pattern : "",
                    name : "Myapp",
                    view : "App",
                    viewPath : "view",
                    targetAggregation: "pages",
                    viewLevel : 1,

                }]

        }
    },

    /**
     * !!! The steps in here are sequence dependent !!!
     */
    init : function () {

        // 1. some very generic requires
        jQuery.sap.require("sap.m.routing.RouteMatchedHandler");
        jQuery.sap.require("sap.ui.demo.Onepage.MyRouter");

        // 2. call overridden init (calls createContent)
        sap.ui.core.UIComponent.prototype.init.apply(this, arguments);

        // 3a. monkey patch the router
        var router = this.getRouter();
        router.myNavBack = sap.ui.demo.Onepage.MyRouter.myNavBack;
        router.myNavToWithoutHash = sap.ui.demo.Onepage.MyRouter.myNavToWithoutHash;

        if (!sap.ui.Device.system.phone) {
            router.myNavToWithoutHash("view.hello", "XML", false);
        }

        // 4. initialize the router
        this.routeHandler = new sap.m.routing.RouteMatchedHandler(router);
        router.initialize();
    },

    destroy : function () {
        if (this.routeHandler) {
            this.routeHandler.destroy();
        }

        // call overridden destroy
        sap.ui.core.UIComponent.prototype.destroy.apply(this, arguments);
    },

    createContent : function () {
        // create root view
        var oView = sap.ui.view({
            id : "app",
            viewName : "view.App",
            type : "XML",
            viewData : { component : this }
        });

        // set navigation model
        // load the global data model
        /*var oJSONDataModel = new sap.ui.model.json.JSONModel("model/data.json");
        oView.setModel(oJSONDataModel);

        // load the global image source model
        var oImgModel = new sap.ui.model.json.JSONModel("model/img.json");
        oView.setModel(oImgModel, "img"); */

        // done
        return oView;
    }
});
在我从任何控制器调用
mynavtowhithouthash
函数之前,一切都正常。我得到的错误是:

Uncaught TypeError: Cannot read property 'addPage' of undefined - MyRouter.js :27

我找不到可能的问题。请帮助。

您的错误似乎与找不到应用程序相关

    var oapp = sap.ui.getCore().byId("Myapp");
您需要在App.view.xml中为控制器类声明controllerName属性

<mvc:View height="100%" xmlns:mvc="sap.ui.core.mvc" controllerName="view.App" 
      xmlns:l="sap.ui.layout" xmlns:core="sap.ui.core" xmlns="sap.m"> 
    <App id="Myapp" />
</mvc:View>

XMLViews自动为ID添加前缀。因此,DOM中sap.m.App的真实ID可能类似于“\uu xmlview0--MyApp”,而“\uu xmlview0”则是视图ID(如果有)(只需签入元素选项卡)。使用
sap.ui.getCore().byId()
查询控件将不知道/不尊重任何前缀。它搜索id“MyApp”,显然是!=“_xmlview0--MyApp”。幸运的是,您的XMLView具有id“app”,因此您可以这样做:

sap.ui.getCore().byId(“app--Myapp”)

所以请记住:无论何时使用XMLViews,都不能在视图控制器中使用
sap.ui.getCore().byId('MyApp')
,而必须使用
this.getView().byId('MyApp')

此外,请注意,只需在路由配置中声明,即可使用自定义路由器类:

metadata : {
        routing : {
            config : {
                routerClass: sap.ui.demo.Onepage.MyRouter,
                viewType : "XML",
                viewPath : "view",
                targetControl : "Myapp",
                targetAggregation: "pages",
                clearTarget : false,
                transition: "slide"
            }
            ...
正如您可能已经猜到的,您的路由器因此需要扩展
sap.ui.core.routing.Router

高频
Christian

MyNavTo的哪一行没有哈希函数?我稍微修改了代码。现在错误出现在第27行(MyRouter.js)。当我删除router.mynavtowhithouthash(“view.hello”,“XML”,false)时;从Component.js(第47行)中,错误消失了!你能给我看看你的App.view.js代码吗?App.view.xml:我不想要splitapp视图。即使我做了这些更改,错误仍然存在。我删除了targetaggregation,因为我想这里不需要它。错误依然存在。嗨,我误解了。我刚发现你不在乎我的属性。