SAPUI5:获取未定义的参数,在从一个视图路由到另一个视图时传递
SAPUI5: Getting argument as undefined, passed while routing from one view to another
我在路由时将参数从 Master
传递到 Detail
视图,但未定义。
代码来自 MasterController.js
onPressItemDetail: function(evt) {
var object = evt.getSource().
getBindingContext().
getModel().
getProperty(evt.getSource().getBindingContext().getPath());
var context = {
object: object,
bindingContext: evt.getSource().getBindingContext()
};
var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
oRouter.navTo("fourth", context);
}
代码来自 DetailController.js
onInit: function() {
var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
oRouter.attachRouteMatched(function(oEvent) {
if (oEvent.getParameter("name") !== "fourth") {
return;
}
var object = oEvent.getParameter("arguments").object;
var bindingContext = oEvent.getParameter("arguments").bindingContext;
}, this);
}
来自Component.js
的路由配置
"routing": {
"config": {
"routerClass": "sap.m.routing.Router",
"viewType": "XML",
"viewPath": "umicoreMP.view",
"controlId": "app",
"controlAggregation": "pages"
},
"routes": [{
"pattern": "",
"name": "first",
"target": "first"
}, {
"pattern": "secondview",
"name": "second",
"target": "second"
}, {
"pattern": "thirdview",
"name": "third",
"target": "third"
}, {
"pattern": "changeitem",
"name": "fourth",
"target": "fourth"
}],
"targets": {
"first": {
"viewName": "FirstView"
},
"second": {
"viewName": "SecondView"
},
"third": {
"viewName": "ThirdView"
},
"fourth": {
"viewName": "ChangeItem"
}
}
}
路由模式必须包含以下格式的所需参数:
{
"pattern": "changeitem/{object}",
"name": "fourth",
"target": "fourth"
}
oRouter.navTo()
包含所需的路由和包含路由参数的对象:
oRouter.navTo("fourth", {object: property});
不推荐将 bindingcontext 作为参数传递(在这种情况下,不可能),因为它是一个对象,在 URL.
中传递这样的对象是不安全的
如果您想访问属于传递给详细信息视图的 属性 的数据,您可以从模型中获取它。
假设您的模型已分配给组件,每个视图都可以访问它。这里可以通过多种方式将模型的正确入口绑定到视图,例如:
oRouter.attachRouteMatched(function (oEvent) {
if (oEvent.getParameter("name") !== "fourth") {
return;
}
var object = oEvent.getParameter("arguments").object;
this.getView().bindElement({path: object, model: "nameOfTheModel"});
}
路径的确切值取决于模型(JSONModel 或 ODataModel)。型号名称应在 component.js
.
中定义
通过这个 bindElement
函数调用,您可以将模型的特定行分配给视图,因此视图中的控件可以使用相对绑定访问所选数据条目的属性:
<Text text="{propertyInModel}" />
我在路由时将参数从 Master
传递到 Detail
视图,但未定义。
代码来自 MasterController.js
onPressItemDetail: function(evt) {
var object = evt.getSource().
getBindingContext().
getModel().
getProperty(evt.getSource().getBindingContext().getPath());
var context = {
object: object,
bindingContext: evt.getSource().getBindingContext()
};
var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
oRouter.navTo("fourth", context);
}
代码来自 DetailController.js
onInit: function() {
var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
oRouter.attachRouteMatched(function(oEvent) {
if (oEvent.getParameter("name") !== "fourth") {
return;
}
var object = oEvent.getParameter("arguments").object;
var bindingContext = oEvent.getParameter("arguments").bindingContext;
}, this);
}
来自Component.js
"routing": {
"config": {
"routerClass": "sap.m.routing.Router",
"viewType": "XML",
"viewPath": "umicoreMP.view",
"controlId": "app",
"controlAggregation": "pages"
},
"routes": [{
"pattern": "",
"name": "first",
"target": "first"
}, {
"pattern": "secondview",
"name": "second",
"target": "second"
}, {
"pattern": "thirdview",
"name": "third",
"target": "third"
}, {
"pattern": "changeitem",
"name": "fourth",
"target": "fourth"
}],
"targets": {
"first": {
"viewName": "FirstView"
},
"second": {
"viewName": "SecondView"
},
"third": {
"viewName": "ThirdView"
},
"fourth": {
"viewName": "ChangeItem"
}
}
}
路由模式必须包含以下格式的所需参数:
{
"pattern": "changeitem/{object}",
"name": "fourth",
"target": "fourth"
}
oRouter.navTo()
包含所需的路由和包含路由参数的对象:
oRouter.navTo("fourth", {object: property});
不推荐将 bindingcontext 作为参数传递(在这种情况下,不可能),因为它是一个对象,在 URL.
中传递这样的对象是不安全的如果您想访问属于传递给详细信息视图的 属性 的数据,您可以从模型中获取它。
假设您的模型已分配给组件,每个视图都可以访问它。这里可以通过多种方式将模型的正确入口绑定到视图,例如:
oRouter.attachRouteMatched(function (oEvent) {
if (oEvent.getParameter("name") !== "fourth") {
return;
}
var object = oEvent.getParameter("arguments").object;
this.getView().bindElement({path: object, model: "nameOfTheModel"});
}
路径的确切值取决于模型(JSONModel 或 ODataModel)。型号名称应在 component.js
.
通过这个 bindElement
函数调用,您可以将模型的特定行分配给视图,因此视图中的控件可以使用相对绑定访问所选数据条目的属性:
<Text text="{propertyInModel}" />