哈希格式错误!使用路由

时间:2015-07-15 14:30:17

标签: sapui5

我开发了一个OpenUI5应用程序,它运行良好!

每次调用路由时都会收到此消息:

2015-07-15 16:15:45 hash format error! The current Hash: /line/01 -    
  log   
  error 
  onHashChange  
  detectHashChange  
  jQuery.event.dispatch 
  jQuery.event.add.elemData.handle  

enter image description here

这不是阻塞问题,但它很烦人,因为它很脏并且填满了调试控制台..!

要拨打我写的路由器:

this.router = sap.ui.core.UIComponent.getRouterFor(this);
this.router.navTo("activities", {
            "id_line": '01'
        });

这是路由文件:

routes: [
                ...
                {
                    pattern: "line/{id_line}",
                    name: "activities",
                    target: ["master_search", "detail_activities"]
                },
                ...
        ],

 targets: {
                master_search: {
                    viewName: "UniversalMenu",
                    viewLevel: 1,
                    controlAggregation: "masterPages"
                }
                ,
                detail_activities: {
                    viewName: "DetailActivity",
                    viewLevel: 4

                }
                ...
            }

修改:这是我使用jQuery.sap.history

的代码段
jQuery.sap.require("jquery.sap.history");
jQuery.sap.require("sap.m.InstanceManager");

sap.ui.controller("ui5bp.view.App", {

    getDefaultPage : function () {
        return "Menu";
    },

    onInit : function () {
        var historyDefaultHandler = function (navType) {
            if (navType === jQuery.sap.history.NavType.Back) {
                //this.navBack(this.getDefaultPage());
            } else {
                this.navTo(this.getDefaultPage(), null, false);
            }
        };

        var historyPageHandler = function (params, navType) {
            if (!params || !params.id) {
                jQuery.sap.log.error("invalid parameter: " + params);
            } else {
                if (navType === jQuery.sap.history.NavType.Back) {
                    this.navBack(params.id);
                } else {
                    this.navTo(params.id, params.data, false);
                }
            }
        };

        jQuery.sap.history({
            routes: [{
                // This handler is executed when you navigate back to the history state on the path "page"
                path : "page",
                handler : jQuery.proxy(historyPageHandler, this)
            }],
            // The default handler is executed when you navigate back to the history state with an empty hash
            defaultHandler: jQuery.proxy(historyDefaultHandler, this)
        });

        // subscribe to event bus
        var bus = sap.ui.getCore().getEventBus();
        bus.subscribe("nav", "to", this.navHandler, this);
        bus.subscribe("nav", "back", this.navHandler, this);
        bus.subscribe("nav", "virtual", this.navHandler, this);
    },

    navHandler: function (channelId, eventId, data) {
        if (eventId === "to") {
            this.navTo(data.id, data.data, true);
        } else if (eventId === "back") {
        //**************************************************
//          if(data && data.id){
//              this.navBack(data.id);
//          } else {
//              jQuery.sap.history.back();
//          }
            var app = this.getView().app;
            if(data.type==="master"){
                app.backMaster();

            }else if(data.type==="detail"){
                app.backDetail();

            }else{alert("back to master o detail?");};
        //**************************************************
        } else if (eventId === "virtual") {
            jQuery.sap.history.addVirtualHistory();
        } else {
            jQuery.sap.log.error("'nav' event cannot be processed. There's no handler registered for event with id: " + eventId);
        }
    },

    navTo : function (id, data, writeHistory) {

        if (id === undefined) {

            // invalid parameter
            jQuery.sap.log.error("navTo failed due to missing id");

        } else {

            var app = this.getView().app;
            // navigate in the app control
            app.to(id, "slide", data);

        }
    },

    /*
    navBack : function (id) {

        if (!id) {

            // invalid parameter
            jQuery.sap.log.error("navBack - parameters id must be given");

        } else {

            // close open popovers
            if (sap.m.InstanceManager.hasOpenPopover()) {
                sap.m.InstanceManager.closeAllPopovers();
            }

            // close open dialogs
            if (sap.m.InstanceManager.hasOpenDialog()) {
                sap.m.InstanceManager.closeAllDialogs();
                jQuery.sap.log.info("navBack - closed dialog(s)");
            }

            // ... and navigate back
            var app = this.getView().app;
            var currentId = (app.getCurrentPage()) ? app.getCurrentPage().getId() : null;
            if (currentId !== id) {
                app.backToPage(id);
                jQuery.sap.log.info("navBack - back to page: " + id);
            }
        }
    }
    */
});

1 个答案:

答案 0 :(得分:1)

在Component.js中,我有两行设置自定义myNavBack和myNavToWithoutHash函数:

// 3a. monkey patch the router
var oRouter = this.getRouter();
oRouter.myNavBack = ui5bp.MyRouter.myNavBack; //to comment
oRouter.myNavToWithoutHash = ui5bp.MyRouter.myNavToWithoutHash; //to comment

我已经从我的应用程序的app骨架示例开始,然后我使用框架中建议的逻辑实现了路由。 两种不同导航方法的共存在控制台中产生了错误。 Tahnkyou @TimGerlach
在两行的评论错误消失后。