为什么我不能在Elm 0.19中使用`onWithOptions`

时间:2018-09-27 16:34:22

标签: elm

我正在尝试将Elm应用程序从0.18升级到0.19。

我被这个错误困扰-

Detected errors in 1 module.                                         
-- BAD IMPORT ---------------------------------------- src/Views/Interaction.elm

The `Html.Events` module does not expose `onWithOptions`:

13| import Html.Events exposing (onWithOptions)
                                 ^^^^^^^^^^^^^
These names seem close though:

    onMouseEnter
    onMouseLeave
    onMouseOut
    onMouseOver

documentation显示onWithOptions应该可用。

我的代码是

module Views.Interaction exposing (onClickNoBubble)

{-| Helper functions for page interactions.


# Helpers

@docs onClickNoBubble

-}

import Html
import Html.Events exposing (onWithOptions)
import Json.Decode as Decode


{-| Replicates the onClick function but prevents bubbling
-}
onClickNoBubble : msg -> Html.Attribute msg
onClickNoBubble message =
    onWithOptions "click" { stopPropagation = True, preventDefault = True } (Decode.succeed message)

我该如何前进?

2 个答案:

答案 0 :(得分:13)

Elm 0.19不使用elm-lang/html。您正在阅读错误的文档。它已被elm/html取代,后者具有一个custom函数,该函数具有相同的作用:

onClickNoBubble : msg -> Html.Attribute msg
onClickNoBubble message =
    Html.Events.custom "click" (Decode.succeed { message = message, stopPropagation = True, preventDefault = True })

答案 1 :(得分:1)

我做了一些辅助功能,以使其正常工作。

onCustomClick : msg -> Html.Attribute msg
onCustomClick msg =
    custom "click"
        (Decode.succeed
            { message = msg
            , stopPropagation = True
            , preventDefault = True
            }
        )