ELM获取查询参数作为字符串 [英] ELM get query parameter as string
问题描述
基于
问题是,您在不会解析URL的路径部分,这主要是 Url.Parser
的目的。您必须完全匹配路径。
以下是一个与您的URL匹配的解析器:
s的 src< /> s Main.elm<?> (Query.string名称)
请注意,解析查询字符串是可选的,这将还匹配您的URL:
s src< /> s Main.elm
但是只要您包含查询参数解析器,它也必须匹配。
如果您只关心查询参数,则必须通过编写自己的函数或使用以下方法来专门解析查询字符串:像 qs 之类的库:
QS.parse
QS.config
?a = 1& b = x
== Dict.fromList
[( a,一个< |数字1)
,( b,一个< | Str x)
]
Based on this post and thanks to the @glennsl iam getting some where.
First if someone has a link that i could learn about the parses i will be very glad.
page : Url.Url -> String
page url =
case (Parser.parse (Parser.query (Query.string "name")) url) of
Nothing -> "My query string: " ++ (Maybe.withDefault "empty" url.query)
Just v -> case v of
Just v2 -> "Finnaly a name"
Nothing -> "????"
As far i can understand the expression Parser.parse (Parser.query (Query.string "name")) url
is returning a Maybe (Maybe String)
I see this as the parser could return something, and if do it could be an string, is that right?
In my mind if i have the parameter name
in my url then my first Just
would be executed and then i can get the name.
But no mather what i put on my url it always go the the first Nothing
The result i got
The problem is that you're not parsing the path part of the URL, which is what Url.Parser
is primarily for. You have to match the path exactly.
Here's a parser that will match your URL:
s "src" </> s "Main.elm" <?> (Query.string "name")
Note also that parsing the query string is optional, meaning this will also match your URL:
s "src" </> s "Main.elm"
But as long as you include a query param parser, that also has to match.
If all you care about is the query parameter, you'll have to parse the query string specifically, by either writing your own function to do so, or using a library like qs for example:
QS.parse
QS.config
"?a=1&b=x"
== Dict.fromList
[ ( "a", One <| Number 1 )
, ( "b", One <| Str "x" )
]
这篇关于ELM获取查询参数作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!