代码之家  ›  专栏  ›  技术社区  ›  Mark Karavan

Elm 0.18中无请求的HTTP请求

elm
  •  0
  • Mark Karavan  · 技术社区  · 7 年前

    我想做一些非常不实用的事情,在elm中发出HTTP请求,而不处理任何类型的响应。基本上是这样的:

    testView : Html Msg
    testView =
      div [] [
        button [onClick TestAction] [text "Test Action"]
      ]
    

    update : Msg -> Model -> ( Model, Cmd Msg )
    update msg model =
        case msg of
          ...
            TestAction ->
              ( model, testActionCmd )
          ...
    

    import Http
    import HttpBuilder exposing (..)
    ...
    testActionCmd : Cmd Msg
    testActionCmd =
      ( "http://localhost:4000/fakeurl" )
        |> get  -- this is a side effect; unrelated to the Msg below
      Cmd.none  -- this is what I want to return
    

    1 回复  |  直到 7 年前
        1
  •  5
  •   Emmanuel Rosa    7 年前

    简而言之,不,您将无法做到这一点(除非编写自己的效果管理器或使用端口)。

    “问题”是 Http 模块允许您创建 Task 然后需要将其转换为 Cmd 执行任务。但是从一个 命令 您需要提供 Msg http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Task

    所以你需要做的是创建一个 努普