路由器navTo后未呈现SAPUI5 / OpenUI5视图

时间:2015-09-27 20:23:32

标签: view rendering router sapui5

我正在创建具有简单路由的SAPUI5示例应用程序(SAPUI5 / OpenUI5 v.1.22)。

我的主要问题是,我试图理解,为什么会触发URL模式更改和目标视图控制器的onInit,但是在没有任何反应(onAfterRendering未被触发)之后,我只能在页面重新加载后才能转到另一个页面。

路由设置。初始化路由器的Compontent.js按以下方式构建:

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

    return UIComponent.extend("sge.apps.app.Component", {

        metadata:{
            name : "Sample App",
            version : "1.0",
            includes : [],
            dependencies : {
                libs : ["sap.m", "sap.ui.layout"],
                components : []
            },

            rootView: "sge.apps.app.view.App",

            config: {
                resourceBundle: "i18n/i18n.properties"
            },

            routing : {
                config : {
                    routerClass : sap.ui.core.routing.Router,
                    viewType : "XML",
                    viewPath : "sge.apps.app.view",
                    targetControl: "app",
                    targetAggregation: "pages",
                    transition: "slide",
                    clearTarget : false,
                    bypassed: {
                        target: "notFound"
                    }
                },
                routes: [{
                    pattern: "",
                    name: "appHome",
                    view: "Home"
                },{
                    pattern : ":all*:",
                    name : "catchallDetail",
                    view : "NotFound",
                    transition : "show"
                },{
                    pattern: "notFound",
                    name: "appNotFound",
                    view: "NotFound",
                    transition : "show"
                }]
            }
        },

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

            var mConfig = this.getMetadata().getConfig();

            // always use absolute paths relative to our own component
            // (relative paths will fail if running in the Fiori Launchpad)
            var rootPath = jQuery.sap.getModulePath("sge.apps.app");

            // set i18n model
            var i18nModel = new sap.ui.model.resource.ResourceModel({
                bundleUrl : [rootPath, mConfig.resourceBundle].join("/")
            });
            this.setModel(i18nModel, "i18n");

            // set device model
            var deviceModel = new sap.ui.model.json.JSONModel({
                isTouch : sap.ui.Device.support.touch,
                isNoTouch : !sap.ui.Device.support.touch,
                isPhone : sap.ui.Device.system.phone,
                isNoPhone : !sap.ui.Device.system.phone,
                listMode : sap.ui.Device.system.phone ? "None" : "SingleSelectMaster",
                listItemType : sap.ui.Device.system.phone ? "Active" : "Inactive"
            });
            deviceModel.setDefaultBindingMode("OneWay");
            this.setModel(deviceModel, "device");

            this.getRouter().initialize();
        }
    });
});

我有 Home.view.xml Home.controller.js ,我尝试导航到另一个视图,按下事件{{1 }}:

onDisplayNotFound

BaseController.js

sap.ui.define([
    "sge/apps/app/controller/BaseController"
], function (BaseController) {
    "use strict";

    return BaseController.extend("sge.apps.app.controller.Home", {
        onDisplayNotFound : function (oEvent) {
            this.getRouter().navTo("appNotFound");
        }
    });
});
目标视图的

NotFound.controller.js NotFound.view.xml

sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/core/routing/History"
], function (Controller, History) {
    "use strict";

    return Controller.extend("sge.apps.app.controller.BaseController", {        
        getRouter: function () {
            return sap.ui.core.UIComponent.getRouterFor(this);
        },
        onNavBack: function (oEvent) {
            var oHistory, sPreviousHash;

            oHistory = History.getInstance();
            sPreviousHash = oHistory.getPreviousHash();

            if(sPreviousHash !== undefined) {
                window.history.go(-1);
            } else {
                this.getRouter().navTo("appHome", {}, true /*no history*/);
            }
        }
    });
});

3 个答案:

答案 0 :(得分:6)

我遇到了同样的问题,我通过在路由配置中添加这一行来解决这个问题:

    "routerClass" : "sap.m.routing.Router",

它完美导航。

"routing": {
        "config": {
            "routerClass" : "sap.m.routing.Router", 
            "viewPath": "es.seidor.view",
            "controlId": "App",
            "async" : "true",
            "clearTarget" : "true"
        }

答案 1 :(得分:0)

sap.ui.define是UI5 v1.30

的一项功能

将您正在使用的版本更新为1.30.x或删除sap.ui.define代码并将其替换为适用于早期版本的代码。

Pre-sap.ui.define代码如下所示:

jQuery.sap.require("sap.m.Button");
//use jQuery.sap.require to require any controls or other files
sap.ui.controller("my.controller", {

    onInit: function(){
        //your code here
        //doing something with sap.m.Button, won't work without the require
        //var oBtn = new sap.m.Button("myBtn", {text: "Click me"});
    },

    onAfterRendering: function(){
        //more code
    }
});

试试。

答案 2 :(得分:0)

解决方案很简单,只需使用TDG最佳实践的某些部分:

创建文件MyRouter.js

sap.ui.define([
    "sap/ui/core/routing/Router",
    "sap/m/routing/RouteMatchedHandler"
], function (Router, RouteMatchedHandler) {
    "use strict";

    return Router.extend("sge.apps.notespese.MyRouter", {
        constructor : function() {
            sap.ui.core.routing.Router.apply(this, arguments);
            this._oRouteMatchedHandler = new sap.m.routing.RouteMatchedHandler(this);
        },
        destroy : function() {
            sap.ui.core.routing.Router.prototype.destroy.apply(this, arguments);
            this._oRouteMatchedHandler.destroy();
        }
    });
});

将它注入Component.js,如下所示:

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

    return UIComponent.extend("sge.apps.notespese.Component", {
    ...

在组件元数据部分替换

routing : {
                config : {
                    routerClass : sap.ui.core.routing.Router,

routing : {
                config : {
                    routerClass : sge.apps.notespese.MyRouter,

希望不要忘记其他与此相关的问题。