typeerror - 类型不匹配 - 沙箱的第一个参数不是我所期望的

标签 typeerror elm elm-architecture

我正在尝试添加订阅,因为我有一个下拉列表,这有助于确保当您在下拉列表之外单击时自动关闭下拉列表。这样做时,我不得不更改 model以及我的 update .

link (将带您到 Elm Bootstrap 站点)是我正在使用的使用 Bootstrap 4 的下拉列表。

我收到的错误

The 1st argument to sandbox is not what I expect:

295| Browser.sandbox 296|> { init = initialModel 297|>
, update = update 298|> , view = view 299|> }

This argument is a record of type:

{ init : ( Model, Cmd Msg )
, update : Msg -> Model -> ( Model, Cmd Msg )
, view : Model -> Html Msg
}

But sandbox needs the 1st argument to be:

{ init : ( Model, Cmd Msg )
, update : Msg -> ( Model, Cmd Msg ) -> ( Model, Cmd Msg )
, view : ( Model, Cmd Msg ) -> Html Msg
}


别名模型
type alias Model =
    { currentNumber : Int, clicks : Int, outputList : List(String), uniqueValues : Dict Int Int, firstNumber : String, secondNumber : String, myDropState : Dropdown.State, items : List String, selectedItem : String, dictKeyToRemove : String,
    modalVisibility : Modal.Visibility  }

初始型号
initialModel : (Model, Cmd Msg)
initialModel =
    ({ currentNumber = 0, clicks = 0, outputList = [""], uniqueValues = Dict.empty, firstNumber = "", secondNumber = "", myDropState = Dropdown.initialState, items = ["Small", "Medium", "Large"], selectedItem = "Small", dictKeyToRemove = "",
    modalVisibility = Modal.hidden }, Cmd.none)


main : Program () Model Msg
main =
    Browser.sandbox
        { init = initialModel           
        , update = update      
        , view = view   
        }

订阅
subscriptions : Model -> Sub Msg
subscriptions model =
    Sub.batch
        [ Dropdown.subscriptions model.myDropState DropMsg ]

更新
update : Msg -> Model -> ( Model, Cmd Msg)
update msg model =
    case msg of   
        DropMsg state ->
            ({model | myDropState = state }, Cmd.none)

我不确定此时我缺少什么,我尝试改变论点但没有运气。

最佳答案

Browser.sandbox将创建一个简单且非常有限的程序。下拉菜单需要更多功能,即订阅,这意味着您需要使用 Browser.elementBrowser.document反而。
Browser.element的类型是:

element :
    { init : flags -> ( model, Cmd msg )
    , view : model -> Html msg
    , update : msg -> model -> ( model, Cmd msg )
    , subscriptions : model -> Sub msg
    }
    -> Program flags model msg

Browser.sandbox 相比:

sandbox :
    { init : model
    , view : model -> Html msg
    , update : msg -> model -> model
    }
    -> Program () model msg

这里有三个区别:
  • init接受一个参数,flags ,它可以是任何东西,运行时会根据它的类型来解释。为了您的目的,只需使用 ()应该足够了(这基本上是 sandbox 所做的),但请参阅 the flags section of the guide更多细节。
  • initupdate返回 ( model, Cmd msg )而不仅仅是 model .这是您错误的根本原因,因为您有 updateinit返回 ( model, Cmd msg ) 的函数如 element会期待,但尝试将它们喂给 sandbox .这让编译器不高兴,因为它认为 model应该是 ( Model, Cmd msg )而不仅仅是 Model .
  • element期待额外的 subscriptions函数,您已定义但目前未执行任何操作,因为沙箱不接受它。

  • 将所有这些放在一起,替换以下内容 main功能应该适合你:
    main : Program () Model Msg
    main =
        Browser.element
            { init = \() -> initialModel           
            , update = update      
            , view = view
            , subscriptions = subscriptions  
            }
    

    关于typeerror - 类型不匹配 - 沙箱的第一个参数不是我所期望的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56401138/

    相关文章:

    ios - 无法将类型 'SlideAnimation.Type' 的值分配给类型“UIViewControllerTransitioningDelegate”?

    javascript - 调用对象方法给出 "Uncaught TypeError: is not a function"错误

    html - 不带参数的 View 函数的正确类型注释

    task - 如何等待thenable链上第一个到达的结果?

    python - TypeError:只能将元组(不是 "float")连接到元组,密度公式有问题

    javascript - 类型错误: "TypeError: function name is not a function at HTMLButtonElement.onclick (/:2:54)"

    elm - Elm 中如何自动将 CSRF Token 转换为 HTTP 请求头?

    Elm:在 HTML 选择中保留值的类型?

    architecture - elm 状态转换消息

    elm - 为什么 Elm 架构被称为 TEA?