2017-01-30 69 views
1

我在此处关注tutorial,并且遇到了使用参数进行路由的问题。使用参数进行路由不起作用

示例应用程序未在本地使用,因此我将其更改为使用本地数据。但是,当我单击发票列表中的某个元素时,出现错误“Uncaught Error:Invalid value”Invoices/1“for segment”{invoicePath}“”。它应该打开一个新的详细信息页面并显示产品名称和金额。

这里是我的路由清单:

"routing": { 
     "config": { 
     "routerClass": "sap.m.routing.Router", 
     "viewType": "XML", 
     "viewPath": "sap.ui.demo.wt.view", 
     "controlId": "app", 
     "controlAggregation": "pages" 
     }, 
     "routes": [ 
     { 
      "pattern": "", 
      "name": "overview", 
      "target": "overview" 
     }, 
     { 
      "pattern": "detail/{invoicePath}", 
      "name": "detail", 
      "target": "detail" 
     } 
     ], 
     "targets": { 
     "overview": { 
      "viewName": "Overview" 
     }, 
     "detail": { 
      "viewName": "Detail" 
     } 
     } 
    } 

Invoices.json示例数据:

{ 
    "Invoices": [ 
    { 
     "ProductName": "Pineapple", 
     "Quantity": 21, 
     "ExtendedPrice": 87.2000, 
     "ShipperName": "Fun Inc.", 
     "ShippedDate": "2015-04-01T00:00:00", 
     "Status": "A" 
    } 
    ] 
} 

InvoiceList.controller.js。在哪里填充发票清单并调用视图更改。

sap.ui.define([ 
    "sap/ui/core/mvc/Controller", 
    "sap/ui/model/json/JSONModel", 
    "sap/ui/demo/wt/model/formatter", 
    "sap/ui/model/Filter", 
    "sap/ui/model/FilterOperator" 
], function (Controller, JSONModel, formatter, Filter, FilterOperator) { 
    "use strict"; 

    return Controller.extend("sap.ui.demo.wt.controller.InvoiceList", { 

     onPress: function (oEvent) { 
      var oItem = oEvent.getSource(); 
      var oRouter = sap.ui.core.UIComponent.getRouterFor(this); 
      oRouter.navTo("detail", { 
       invoicePath: oItem.getBindingContext("invoice").getPath().substr(1) 
      }); 
     } 
    }); 

}); 

回答

2

该错误消息是由所述路由器库提高。路由被定义为detail/{invoicePath},并且您通过Invoice/1作为参数,由于该参数包含被视为URL段分隔符的斜杠,因此不允许该参数。

但是,您提到您无法在本地运行该示例并执行了一些收养操作。该路径看起来像您正在使用JSONModel。这意味着你也需要在你的例子中采用几个部分。

InvoiceList控制器:

oItem.getBindingContext("invoice").getPath().substr(10) 

结合上下文应该是/Invoices/1和要传递的唯一指标。因此您需要切断/Invoices/

详细控制器:

_onObjectMatched: function (oEvent) { 
    this.getView().bindElement({ 
     path: "/Invoices/"+ oEvent.getParameter("arguments").invoicePath, 
     model: "invoice" 
    }); 
} 

这将绑定在相应的模型视图以/Invoices/1

0

@matbtt的答案是对的。

另一个解决方案是删除PATH。否需要substr和特殊offset

encodeURIComponent(oItem.getBindingContext("invoice").getPath())

_onObjectMatched: function (oEvent) { 
    this.getView().bindElement({ 
     path: decodeURIComponent(oEvent.getParameter("arguments").invoicePath), 
     model: "invoice" 
    }); 
} 

两个JSON模式和OData的将工作做好。