视图列表中的 Elm 不匹配

Elm mismatch in view list

type Msg
    = NoOp
    | RequestDate
    | ReceiveDate Date
    | UpdateYouTubeUrl YouTubeUrl

-- -

root : Maybe YouTubeUrl -> Html Msg
root youTubeUrl =
    case youTubeUrl of
        Just youTubeUrl ->
            div []
                [ player youTubeUrl
                , urlInput
                ]

        Nothing ->
            urlInput


player : YouTubeUrl -> Html Msg
player youTubeUrl =
    h1 [] [ text ("YouTube player for " ++ youTubeUrl ++ " goes here") ]


urlInput : Html (YouTubeUrl -> Msg)
urlInput =
    input
        [ placeholder "(Enter a YouTube embed URL and hit <Enter>)"
        , onSubmit UpdateYouTubeUrl
        ]
        []

这给我一个类型不匹配错误:

-- TYPE MISMATCH ---------------------------------- ./src/YouTubePlayer/View.elm

The 1st and 2nd entries in this list are different types of values.

28|                 [ player youTubeUrl
29|>                , urlInput
30|                 ]

The 1st entry has this type:

    Html (Msg)

But the 2nd is:

    Html (YouTubeUrl -> Msg)

Hint: It looks like a function needs 1 more argument.

Hint: Every entry in a list needs to be the same type of value. This way you
never run into unexpected values partway through. To mix different types in a
single list, create a "union type" as described in:
<http://guide.elm-lang.org/types/union_types.html>

Detected errors in 1 module. 

错误很明显,问题是我有一个列表

[ Html Msg
, Html (YouTubeUrl -> Msg)
]

... Elm 中的列表需要同质,但我不明白的是为什么 Html (YouTubeUrl -> Msg)urlInput 的类型签名。我认为这与我使用 onSubmit 有关。

我是这里的 Elm 新手,所以我不确定自己做错了什么。 Elm Guide book 没有任何类型示例看起来像我看到的 Html (a -> Msg)


elm 0.18

问题出在您使用onSubmit,其签名是这样的:

onSubmit : msg -> Attribute msg

您正在向它传递一个构造函数 UpdateYouTubeUrl,它接受一个参数。为了使用 UpdateYouTubeUrl 生成 Msg,您必须传递类型为 YouTubeUrl 的参数。该参数未被传递,因此 Elm 的编译器告诉您 urlElement 函数需要一个 YouTubeUrl 参数。

我的预感是您打算使用 onInput,它接受一个字符串值作为输入,您可以通过获取输入的目标值来使用它。另外,onSubmit 通常在包装输入的表单元素上进行。

在大多数情况下,如果 Elm 编译器告诉您它正在寻找一个 return 值为 Html (a -> msg) 的视图函数,这可能意味着您在函数,因为您的视图通常应该是 Html Msg 类型(当然,更高级的情况除外)。