读取可选的 url 参数 [英] read optional url parameters

查看:67
本文介绍了读取可选的 url 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 sapui5 应用程序中捕获一个可选的 url 参数.

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

清单:

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

控制器:

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

测试用例

根据测试用例模式匹配

  • URLPattern :query:

  • URLPattern :query:

清单模式:查询:

将匹配:{"query":"test=123123,bla=123213"}

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

网址:

/webapp/index.html?test=123

/webapp/index.html?test=123

没有价值:oEvent.mParameters.data.hash:"

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

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

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

有值:oEvent.mParameters.data.hash:query="123"

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

  • 为什么我必须添加 #/?
  • 我怎样才能避免添加 #/?

推荐答案

我认为您在理解 UI5 路由概念时失败了.您不应将其理解为 GET 请求.你可以在hash路由中传递的参数不是GET参数.

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.

所以首先,您应该了解 URL 的组成部分,在这种情况下,我们可以这样说:

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`

?# 你有 GET 请求的参数.他们去服务器端.
# 到最后你有你的哈希路由.在客户端使用.

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 --> 它传递 3 作为必需的 paramValue1 而没有传递给可选的 paramValue2

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

#/My/Navigation/Pattern/3/2 --> 它传递 3 作为必需的 paramValue1 和 2 作为可选的 paramValue2>

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

但是如果你尝试

#/My/Navigation/Pattern/

它会失败,因为您已将 paramValue1 设置为强制

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

要获取这些参数,请在您的控制器中为RouteMatched"事件定义一个事件处理程序.参数将在事件对象的参数"中.在此处进行了清楚的解释.

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.

我建议您阅读本教程.它是 UI5 演示套件中最好的教程之一.

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

这篇关于读取可选的 url 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆