最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - read optional url parameters - Stack Overflow

programmeradmin0浏览0评论

I want to catch an optional url parameter in my sapui5 application.

Manifest:

            "routes": [
            {
                "pattern": "page_1:query:",
                "name": "page_1",
                "target": [
                    "page_1"
                ]
            }]

Controller:

    handleRouteMatched: function(oEvent) {
        var oArgs, oView, oQuery;
        oArgs = oEvent.getParameter("arguments"); // undefined
        console.log(oEvent.mParameters);

Testcases

according to the Testcases PatternMatching

  • URLPattern :query:

  • manifest pattern:query:

  • would match: {"query":"test=123123,bla=123213"}

URLs:

/webapp/index.html?test=123

has no value: oEvent.mParameters.data.hash: ""

/webapp/index.html?#/query=123

has value: oEvent.mParameters.data.hash: query="123"

  • why do I have to add #/ ?
  • And how can I avoid adding #/ ?

I want to catch an optional url parameter in my sapui5 application.

Manifest:

            "routes": [
            {
                "pattern": "page_1:query:",
                "name": "page_1",
                "target": [
                    "page_1"
                ]
            }]

Controller:

    handleRouteMatched: function(oEvent) {
        var oArgs, oView, oQuery;
        oArgs = oEvent.getParameter("arguments"); // undefined
        console.log(oEvent.mParameters);

Testcases

according to the Testcases PatternMatching

  • URLPattern :query:

  • manifest pattern:query:

  • would match: {"query":"test=123123,bla=123213"}

URLs:

/webapp/index.html?test=123

has no value: oEvent.mParameters.data.hash: ""

/webapp/index.html?#/query=123

has value: oEvent.mParameters.data.hash: query="123"

  • why do I have to add #/ ?
  • And how can I avoid adding #/ ?
Share Improve this question edited Feb 3, 2018 at 11:29 Cœur 38.8k25 gold badges206 silver badges278 bronze badges asked Jan 18, 2018 at 11:22 MederaMedera 4183 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

I think you are failing when understanding the UI5 Routing concept. You should not understand it as a GET request. The parameters you can pass in the hash route are not GET parameters.

So first, you should understand the parts of a URL, in this case we can say something like this:

<host>:<port>?myGetParam1=value1&myGetParam2=value2#/My/Navigation/Pattern/ParamValue1/ParamValue2`

From the ? to the # you have the parameters for the GET request. They go to the server side.
From the # to the end you have your hash route. Used in the client side.

if you define in the manifest the pattern as

 /My/Navigation/Pattern/{ParamValue1}/:ParamValue2:

then the following examples will work

#/My/Navigation/Pattern/3 --> It passes 3 as the mandatory paramValue1 and nothing for the optional paramValue2

#/My/Navigation/Pattern/3/2 --> It passes 3 as the mandatory paramValue1 and 2 as the optional paramValue2

However if you try

#/My/Navigation/Pattern/

it will fail, because you has set the paramValue1 as mandatory

To get those paramerters, define an event handler in your controller for the "RouteMatched" event. The parameters will be in the "arguments" of the event object. Clearly explained here.

I reend you to go through this tutorial. It is one of the best tutorials in the UI5 demokit.

发布评论

评论列表(0)

  1. 暂无评论