Elm getting started guide似乎没有使用标准的Elm HTML事件,我不知道如何使用它们。例如,onBlur
:
view model =
div []
[ div []
input [ type_ "text", onInput MsgA ] []
, input [ type_ "text", onBlur MsgB ] []
]
这在编译器中失败,因为现在我在第3行返回类型Html (String -> Msg)
,但在第2行返回Html (Msg)
。
为什么这两个事件不兼容?有没有办法同时使用这两种方法?此外,文档还不足以让像我这样的人了解如何使用onBlur
。
答案 0 :(得分:4)
onBlur
的类型签名与onInput
不同。
onInput : (String -> msg) -> Attribute msg
onBlur : msg -> Attribute msg
这意味着您Msg
使用的onInput
必须采用单个字符串参数。同样,Msg
中使用的onBlur
也无法获取参数。如果您将MsgA
和MsgB
重新定义为以下内容,则会编译:
type Msg
= MsgA String
| MsgB
修改强>
如果您希望模糊处理程序能够接受目标值,您可以像这样滚动自己:
import Html.Events exposing (on, targetValue)
import Json.Decode as Json
onBlurWithTargetValue : (String -> msg) -> Attribute msg
onBlurWithTargetValue tagger =
on "blur" (Json.map tagger targetValue)