如何在游乐场更新功能中发出http请求?

问题描述

我想从我的elm程序发出http请求。 我将openapi-generator https://eriktim.github.io/openapi-elm用于http请求: https://github.com/eriktim/openapi-elm, 我能找到的唯一示例是:
https://github.com/eriktim/openapi-elm/tree/master/example
那里有一个请求类型Api.Request Api.Data.PlanetList并使用发送功能(Result Http.Error a -> msg) -> Request a -> Cmd msg进行转换。
send函数使用将请求结果转换为msg的函数,但将其包装在Cmd中返回。 更新功能具有类型 update : Msg -> Model -> ( Model,Cmd Msg ) 因此,只要在update函数中发出请求并将结果放入返回值中,框架就会从msg中获得Cmd

现在我想在程序中发出请求,但是我使用playground game作为主要功能example),其中更新功能update : Computer -> Model -> Model,因此示例项目中的“技巧”不适用。那我怎么还能从请求调用获取值?

解决方法

Http请求是一段数据,供运行时执行。如果Cmd没有通过主update传递给运行时,则实际的http调用将永远不会发生。

这就是为什么您不能在简单程序(PlaygroundBrowser.sandbox)中产生副作用。

,

我不确定是什么榆木游乐场,但这不是您想要创建的Web应用程序的正确起点,因为它不支持Http请求等命令。

您要使用普通/标准榆木-请参见https://guide.elm-lang.org/install/elm.html,然后您要基于Program-https://guide.elm-lang.org/webapps/

构建Browser.document

希望能带您上路