Elm create-elm-app Error: Problem with the flags given to your Elm program on initialization. Json.Decode.oneOf failed in the following 2 ways

Elm create-elm-app Error: Problem with the flags given to your Elm program on initialization. Json.Decode.oneOf failed in the following 2 ways

我正在开发一个 elm 应用程序,当我尝试将 Model 更改为 {} 以外的任何内容时,我收到错误消息

Error: Problem with the flags given to your Elm program on initialization. Json.Decode.oneOf failed in the following 2 ways: (1) Problem with the given value: undefined Expecting null (2) Problem with the given value: undefined Expecting an INT

错误重现here

错误是因为 init 期望 Maybe Model 但在 Ellie 的 HTML 部分你没有传递标志。

有两个选项,您可以处理标志,也可以删除标志。

要保留解析标志: 您需要将 Elm.Main.init({ node: document.querySelector('main') }) 更改为

var app = Elm.Main.init({
  node: document.querySelector('main'),
  flags: <some value>
});

通常还建议将标志更改为 Json.Decode.Value 并手动解码它们 所以你的初始化会变成

init : Value -> ( Model, Cmd Msg )
init flags =
  case Json.Decode.decodeValue flagDecoder flags of
    Ok decodedFlags -> ...
    Err err -> ...

这样你就可以处理无效或丢失的标志。

移除标志解析

常见的方式是给我们单位类型()。 所以你的主要变成

main : Program () Model Msg

init 变为

init : () -> ( Model, Cmd Msg )