我真的很想学习一些 ELM,但是我的思想在查询解析时崩溃了,我的想法是创建一个函数来按名称获取查询字符串值,例如:给定一个查询字符串?name=Neuber
像这样的函数getParam "name"
那会返回Neuber
但它在最基本的例子中失败了,它甚至无法编译
page
comes 从这里 https://package.elm-lang.org/packages/elm/url/latest/Url-Parser-Query#map
routeParser
comes 从这里 https://package.elm-lang.org/packages/elm/url/latest/Url-Parser-Query#map2
module Main exposing (..)
-- import Url.Parser exposing (Parser, (</>), (<?>), oneOf, s)
import Url.Parser.Query exposing (int, map, map2, string)
type alias QueryParams =
{ search : Maybe String
, page : Maybe Int
}
routeParser : Url.Parser.Query.Parser QueryParams
routeParser = map2 QueryParams (string "search") (int "page")
page : Url.Parser.Query.Parser Int
page = map (Result.withDefault 1) (int "page")
我得到的错误
-- TYPE MISMATCH ---------------- /a/long/way/to/project/src/Main.elm
The 2nd argument to `map` is not what I expect:
15| page = map (Result.withDefault 1) (int "page")
^^^^^^^^^^
This `int` call produces:
Url.Parser.Query.Parser (Maybe Int)
But `map` needs the 2nd argument to be:
Url.Parser.Query.Parser (Result x number)
Hint: I always figure out the argument types from left to right. If an argument
is acceptable, I assume it is “correct” and move on. So the problem may actually
be in one of the previous arguments!