【问题标题】:Elm create-elm-app Error: Problem with the flags given to your Elm program on initialization. Json.Decode.oneOf failed in the following 2 waysElm create-elm-app 错误:初始化时赋予 Elm 程序的标志有问题。 Json.Decode.oneOf 以下2种方式失败
【发布时间】:2021-04-13 02:05:08
【问题描述】:

我正在开发一个 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

【问题讨论】:

    标签: model elm flags


    【解决方案1】:

    错误是因为 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 会变成

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

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

    移除标志解析

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

    main : Program () Model Msg
    

    初始化变成

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多