【问题标题】:Error with Maybe and nested resources in Elm榆树中的Maybe和嵌套资源出错
【发布时间】:2016-08-27 10:08:24
【问题描述】:

我正在构建我的第一个 Elm SPA,并将我的组件组织在不同的文件夹和模块中。一切正常,直到我改变了主模型:

type alias Model =
{ contacts : Contacts.Model.Model
, contact : Contact.Model.Model
, route : Routing.Route
}

到这里:

type alias Model =
{ contacts : Contacts.Model.Model
, contact : Maybe Contact.Model.Model
, route : Routing.Route
}

我已经在代码库中进行了所有必要的更改以使其正常工作,但我缺少一些我找不到的东西,因为在主 Update 模块中我经常收到此编译错误:

The type annotation for `update` does not match its definition. - The type annotation is saying:

    Msg
    -> { ..., contact : Maybe Contact.Model.Model }
    -> ( { contact : Maybe Contact.Model.Model
    , contacts : Contacts.Model.Model
    , route : Routing.Route
    }
    , Cmd Msg
    )

But I am inferring that the definition has this type:

    Msg
    -> { ...
    , contact :
          { birth_date : String
          , email : String
          , first_name : String
          , gender : Int
          , headline : String
          , id : Int
          , last_name : String
          , location : String
          , phone_number : String
          , picture : String
          }
    }
    -> ( { contact : Maybe Contact.Model.Model
    , contacts : Contacts.Model.Model
    , route : Routing.Route
    }
    , Cmd Msg
    )

看起来我错过了在某处传递Maybe Model,但我找不到它。这是更新函数的样子:

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
    ContactsMsg subMsg ->
        let
            ( updatedContacts, cmd ) =
                Contacts.Update.update subMsg model.contacts
        in
            ( { model | contacts = updatedContacts, contact = Nothing }, Cmd.map ContactsMsg cmd )

    ContactMsg subMsg ->
        let
            ( updatedContact, cmd ) =
                Contact.Update.update subMsg model.contact
        in
            ( { model | contact = updatedContact }, Cmd.map ContactMsg cmd )

这是完整的 repo,错误:https://github.com/bigardone/phoenix-and-elm/tree/feature/routes-refactoring/web/elm

我做错了什么?非常感谢您!

【问题讨论】:

    标签: elm


    【解决方案1】:

    问题是由Update.update函数中的类型不匹配引起的。

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

    您必须处理 Update.elm 中的 Maybe 值:

    update : Msg -> Model -> ( Model, Cmd Msg )
    update msg model =
        case msg of
            ContactsMsg subMsg ->
                let
                    ( updatedContacts, cmd ) =
                        Contacts.Update.update subMsg model.contacts
                in
                    ( { model | contacts = updatedContacts, contact = Nothing }
                    , Cmd.map ContactsMsg cmd
                    )
    
            ContactMsg subMsg ->
                case model.contact of
                    Just contact ->
                        let
                            ( updatedContact, cmd ) =
                                Contact.Update.update subMsg contact
                        in
                            ( { model | contact = updatedContact }
                            , Cmd.map ContactMsg cmd
                            )
    
                    Nothing ->
                        ( model, Cmd.none )
    

    【讨论】:

    • 嗨@halfzebra,这确实有效,非常感谢!我现在遇到的问题是它没有与Contact.Update.update 函数正确映射,因为当单击联系人的 gravatar 时,它会访问 de show route,运行命令以在后端获取联系人数据但它没有进入内部de FetchContactSucceed 设置模型并在屏幕上显示:(
    • @bigardone 如果您是 Elm 的新手,我建议您查看 simpler examples,您可能从一开始就难以理解。
    猜你喜欢
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 2012-02-09
    • 1970-01-01
    • 2012-03-17
    相关资源
    最近更新 更多