Elm 0.18 中无请求的 HTTP 请求

HTTP request without request in Elm 0.18

我想做一些非常不实用的事情,并在 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

有没有办法在 Elm 中做这样的事情?

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

"problem" 是 Http 模块允许您创建一个 Task 然后您需要将其转换为 Cmd 来执行任务。但是要从 Task 变为 Cmd,您需要提供 Msg。参见 http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Task

所以您需要做的是创建其中一个 Noop 消息。