首页 > 解决方案 > UI5 Routed View 与预期的绑定元素,但无法正确找到路径

问题描述

我正在尝试使用以下数据获取路由视图以显示上一个屏幕中的元素:加载到模型的 JSON 模型locationListModel

{
    value: [
        {
            id: 251,
            locationName: "SAP",
            holidayList: [ ]
        }
    ]
} 

我假设我们可以locationName使用以下路径访问第一个位置:{locationListModel>/value/0/locationName}

因此,当路由到另一个视图时,我们可以像这样访问它,或者通过执行以下操作通过视图中的元素绑定上下文来访问它:

...
    onInit: function() {
        this.getRouter().getRoute("location").attachMatched(this._onObjectMatched, this);
    },
    _onObjectMatched: function(oEvent) {
        this.getView().bindElement({
            path:"/value/" + oEvent.getParameter("arguments").locationId,
            model:"locationListModel"});

    },
...

在当前视图中显示: 当前视图描述

但是,当尝试通过使用在视图中访问它时{locationName}

<Page showNavButton="true" navButtonPress="onNavBack">
    <ObjectHeader title="{locationName}"/> //doesn't show
    <ObjectHeader title="{locationListModel>/value/0/locationName}"/> //shows
</Page>

我们在上面的标题上什么也没有。

这是参考导航和路由演练完成的。我对 UI5 相当陌生,我似乎无法在任何地方找到此信息。我很感激任何帮助。谢谢你。

标签: sapui5

解决方案


在我看来,这个问题的提出是因为您可能参考了练习中的示例代码(上面提到的导航和路由在此处输入图像描述

如果此示例适用于在 oData 的情况下传递 URL 路径,则如果您使用的是 JSON 示例,它将无法正常工作。这会尝试将参数 invoicePath 作为“Invoices/0”发送到路由器,这是错误的,您将收到如下错误: 在此处输入图像描述

发生这种情况是因为路径不希望在两者之间有“/”。这个问题有一个类似的线程:Routing with parameters does not work

您可以通过仅传递路由器中的索引来解决此问题,例如

onPress: function(oEvent) {
  var oItem = oEvent.getSource();
  var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
  oRouter.navTo("detail", {

    //invoicePath should be the index. 
    invoicePath: oItem.getBindingContext("invoice").getPath().substr(10)

  });
}

让我知道这是否有帮助!


推荐阅读