我正在研究一个基本的解码 json Elm 示例以供练习,但无法找出 Elm 编译错误。我也很困惑为什么它在 Ellie 中运行(即使用远程 JSON URL),但在使用 Elm v 19.0 FYI 本地编译时却没有。目标是进行一个简单的调用以从 Go 服务器获取 JSON,但仅编译我从文档中获得的用于解码 JSON 的示例 Elm 并没有成功,所以我们在这里。module HelloWorld exposing (..)import Browserimport Html exposing (..)import Html.Attributes exposing (..)import Html.Events exposing (..)import Httpimport Json.Decode exposing (Decoder, field, string)-- MAINmain = Browser.element { init = init , update = update , subscriptions = subscriptions , view = view }-- MODELtype Model = Failure | Loading | Success Stringinit : () -> (Model, Cmd Msg)init _ = (Loading, getRandomCatGif)-- UPDATEtype Msg = MorePlease | GotGif (Result Http.Error String)update : Msg -> Model -> (Model, Cmd Msg)update msg model = case msg of MorePlease -> (Loading, getRandomCatGif) GotGif result -> case result of Ok url -> (Success url, Cmd.none) Err _ -> (Failure, Cmd.none)-- SUBSCRIPTIONSsubscriptions : Model -> Sub Msgsubscriptions model = Sub.none-- VIEWview : Model -> Html Msgview model = div [] [ h2 [] [ text "Random Cats" ] , viewGif model ]viewGif : Model -> Html MsgviewGif model = case model of Failure -> div [] [ text "I could not load a random cat for some reason. " , button [ onClick MorePlease ] [ text "Try Again!" ] ] Loading -> text "Loading..." Success url -> div [] [ button [ onClick MorePlease, style "display" "block" ] [ text "More Please!" ] , img [ src url ] [] ]-- HTTPgetRandomCatGif : Cmd MsggetRandomCatGif = Http.get { url = "http://127.0.0.1:8080/test" , expect = Http.expectJson GotGif gifDecoder }gifDecoder : Decoder StringgifDecoder = field "data" (field "image_url" string)现在是 go 服务器// write json back to elm callpackage mainimport ( "fmt" "log" "net/http" "os" "github.com/gorilla/schema")type ToDo struct { Title string Description string}
1 回答
侃侃尔雅
TA贡献1801条经验 获得超16个赞
看起来您已经安装了 1.0.0 版本的elm/http
模块,而示例需要elm/http
2.0.0 版本。我可以使用此软件包的 1.0.0 版本重现您的错误,但使用 2.0.0 成功编译的代码。
编辑您的elm.json
文件以更改 to 的版本elm/http
并再次2.0.0
尝试运行elm make ...
。
- 1 回答
- 0 关注
- 87 浏览
添加回答
举报
0/150
提交
取消