SAPUI5中的attachMatched()和attachPatternMatched()和/或attachRouteMatched()和attachRoutePatternMatched()有什么区别?

n01*_*dea 6 sapui5

对于以下SAPUI5路由方法之间的区别有一个示例感到高兴:

sap.ui.core.routing.Route

  • attachMatched()
  • attachPatternMatched()

sap.ui.core.routing.Router

  • attachRouteMatched()
  • attachRoutePatternMatched()

API说明了什么attachMatched()attachPatternMatched()没有任何区别。

API表示attachRouteMatched()

将event-handler附加fnFunctionrouteMatchedthis 的事件上 sap.ui.core.routing.Router

API表示attachRoutePatternMatched()

将event-handler附加fnFunctionroutePatternMatchedthis 的事件上sap.ui.core.routing.Router。此事件类似于路由匹配。但它只会对具有匹配模式的路由触发,而不会针对其父模式触发Routes

例如可以使用

sap.ui.define([
    "sap/ui/core/mvc/Controller"
], function (Controller) {
    "use strict";
    return Controller.extend("sap.ui.demo.wt.controller.Detail", {
        onInit: function () {
            var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
            oRouter.getRoute("detail").attachMatched(this._onObjectMatched, this);              
            // oRouter.attachRouteMatched(this._onObjectMatched, this);
        },
        _onObjectMatched: function (oEvent) {
            this.getView().bindElement({
                path: "/" + oEvent.getParameter("arguments").invoicePath,
                model: "invoice"
            });
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

要么

sap.ui.define([
    "sap/ui/core/mvc/Controller"
], function (Controller) {
    "use strict";
    return Controller.extend("sap.ui.demo.wt.controller.Detail", {
        onInit: function () {
            var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
            oRouter.getRoute("detail").attachPatternMatched(this._onObjectMatched, this);
            // oRouter.attachRoutePatternMatched(this._onObjectMatched, this);
        },
        _onObjectMatched: function (oEvent) {
            this.getView().bindElement({
                path: "/" + oEvent.getParameter("arguments").invoicePath,
                model: "invoice"
            });
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

没什么区别。不要得到«但只会为具有匹配模式的路由触发,而不会为其父路由attachRouteMatch()触发。”思想只会为具有匹配模式的路由触发。

n01*_*dea 5

在这方面的区别是:

  1. sap.ui.core.routing.Routesap.ui.core.routing.Router

sap.ui.core.routing.RouteattachMatchedattachPatternMatched火灾的具体规定路线。在以下路线“详细”中:

let oRouter = sap.ui.core.UIComponent.getRouterFor(this);
oRouter.getRoute("detail").attachMatched(this._onObjectMatched, this);   
Run Code Online (Sandbox Code Playgroud)

sap.ui.core.routing.RouterattachRouteMatchedattachRoutePatternMatched任何航线火灾:

let oRouter = sap.ui.core.UIComponent.getRouterFor(this);            
oRouter.attachRouteMatched(this._onObjectMatched, this);
Run Code Online (Sandbox Code Playgroud)

为了澄清起见:如果为特定路线添加了限制,则sap.ui.core.routing.Router的方法将具有与相同的结果sap.ui.core.routing.Route

_onObjectMatched: function(oEvent) {
    if (oEvent.getParameter("name") !== "detail") {
        …
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,无论如何,都会sap.ui.core.routing.Router开火_onObjectMatched。对详细路由的限制发生在_onObjectMatched带有if子句的fired方法中。仅在按下“详细”路线时才sap.ui.core.routing.Route触发_onObjectMatched

  1. sap.ui.core.routing.RouterattachMatched/ sap.ui.core.routing.RouteattachRouteMatchedsap.ui.core.routing.RouterattachPatternMatched/ sap.ui.core.routing.RouteattachRoutePatternMatched

attachMatched/ attachRouteMatched为匹配的路线开火。attachMatched为任何路由或子路由触发attachRouteMatched为指定路线的匹配触发。


结论:

  • attachPatternMatched/ attachRoutePatternMatched为匹配的子路由触发
  • attachPatternMatched为该路线的路线触发
  • attachRoutePatternMatched为任何匹配的子路由触发。即attachPatternMatched/ attachRoutePatternMatched不开父路线触发。

tl; dr:

  • 的特定路线sap.ui.core.routing.Route
  • 没有特定的路线sap.ui.core.routing.Router
  • attachMatched/ attachRouteMatched沿任何路线触发。
  • attachPatternMatched/ attachRoutePatternMatched触发子路由,而不触发父路由。