我真的很想学一点ELM,但是我的思路在查询解析时就崩溃了,我的想法是创建一个函数,通过名称来获取查询字符串的值,比如:给定一个查询字符串。?name=Neuber
这样的功能 getParam "name"
会回 Neuber
但它在最基本的例子中失败了,它甚至不能编译。
page
来自 从此
routeParser
来自 从此
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!
眼前的问题是 int "page"
将返回一个 Maybe Int
但你却想用它与 Result.withDefault
,正如错误信息所说,它的期望是一个 Result
. 解决这个问题的方法是使用 Maybe.withDefault
而不是。