在SAPUI5中匹配无效哈希时如何显示NotFound

时间:2018-11-06 18:29:59

标签: sapui5

我执行了使用SAPUI5捕获和处理无效哈希的步骤,但是我的应用程序无法正常工作。

enter image description here

当我尝试导航到Not Found视图并更改Hash时,我只会收到一条信息消息:

But

但是没有显示视图。

[编辑]:

添加源代码文件:

在这里我添加了绕过的部分

enter image description here

我已经在清单的“目标”部分中创建了目标:

enter image description here

这是NotFound.controller.js

sap.ui.define([
   "my/path/controller/BaseController"
], function (BaseController) {
"use strict";

return BaseController.extend("my.path.controller.NotFound", {

    onInit: function () {
        var oRouter, oTarget;

        oRouter = this.getRouter();
        oTarget = oRouter.getTarget("NotFound");
        oTarget.attachDisplay(function (oEvent) {
            this._oData = oEvent.getParameter("data");  // store the data
        }, this);
    },

    // override the parent's onNavBack (inherited from BaseController)
    onNavBack : function (oEvent){
        // in some cases we could display a certain target when the back button is pressed
        if (this._oData && this._oData.fromTarget) {
            this.getRouter().getTargets().display(this._oData.fromTarget);
            delete this._oData.fromTarget;
            return;
        }

        // call the parent's onNavBack
        BaseController.prototype.onNavBack.apply(this, arguments);
    }

});

});

这是NotFound.view.xml:

<mvc:View
   controllerName="my.path.controller.NotFound"
   xmlns="sap.m"
   xmlns:mvc="sap.ui.core.mvc">
   <MessagePage
      title="{i18n>NotFound}"
      text="{i18n>NotFound.text}"
      description="{i18n>NotFound.description}"
      showNavButton="true"
      navButtonPress="onNavBack"/>
</mvc:View>

这里是App控制器上的onInit方法:

onInit: function(){
        jQuery.sap.log.setLevel(jQuery.sap.log.Level.INFO);

        var oRouter = this.getRouter();

        oRouter.attachBypassed(function (oEvent) {
            var sHash = oEvent.getParameter("hash");
            // do something here, i.e. send logging data to the backend for analysis
            // telling what resource the user tried to access...
            jQuery.sap.log.info("Sorry, but the hash '" + sHash + "' is invalid.", "The resource was not found.");
        });

        oRouter.attachRouteMatched(function (oEvent){
            var sRouteName = oEvent.getParameter("name");
            // do something, i.e. send usage statistics to backend
            // in order to improve our app and the user experience (Build-Measure-Learn cycle)
            jQuery.sap.log.info("User accessed route " + sRouteName + ", timestamp = " + new Date().getTime());
        });
    }

和 有什么可以帮助我的吗? 问候

1 个答案:

答案 0 :(得分:0)

检查这个矮人:

https://plnkr.co/edit/pxOkRSM8c97hXO6gkbpV

关键配置是manifest.json上的

 "targets": {
                "tgHome": {
                    "viewPath": "sapui5Example",
                    "viewName": "home"
                },
                "notFound": {
                    "viewPath": "sapui5Example",
                    "viewName": "NotFound",
                    "transition": "show"
                }
            }

要触发“未找到”路线,请下载插件,并在URL中,在哈希之后键入任何内容,您将找到未找到的页面(如果直接在插件上进行操作将无法使用)。这是一张照片:

Not Found example