Elm 单击按钮时未调用更新函数

Elm 单击按钮时未调用更新函数,elm,Elm,单击按钮(应发送消息)后,不会调用“我的更新”功能。 以下是应使用onClick事件触发函数的代码: [ input [ type_ "submit", onClick TopicSelected, value (getTopic topic) ] [] topicTocheckbox : Topic -> Html Msg topicTocheckbox topic = div [] [ input [ type_ "submit", onClick Topic

单击按钮(应发送消息)后,不会调用“我的更新”功能。

以下是应使用onClick事件触发函数的代码:

[ input [ type_ "submit", onClick TopicSelected, value (getTopic topic) ] []
topicTocheckbox : Topic -> Html Msg
topicTocheckbox topic =
    div []
        [ input [ type_ "submit", onClick TopicSelected, value (getTopic topic) ] []
        , label [] [ text <| getTopic topic ]
        ]
以下是更新功能:

update : Msg -> Model -> Model
update msg model =
    case msg of
        TopicSelected ->
            { model
                | articles = []
                , podcasts = []
                , videos = []
            }

view model =
    div []
        [ table []
            [ tr [] [ td [] [ b [] [ text "Videos" ] ] ]
            , div [] <| contentUI model.videos
            , tr [] [ td [] [ b [] [ text "Podcasts" ] ] ]
            , div [] <| contentUI model.podcasts
            , tr [] [ td [] [ b [] [ text "Articles" ] ] ]
            , div [] <| contentUI model.articles
            ]
        ]
更新:Msg->Model->Model
更新msg模型=
味精案例
主题选择->
{模型
|条款=[]
,播客=[]
,视频=[]
}
视图模型=
分区[]
[表[]
[tr[][td[][b[][文本“视频”]]

,div[]查看您的代码,您试图在Home.elm中使用Contributor.elm的嵌套“组件”,但您没有在父(主)更新函数中进行连接

这在榆树社区是一个广泛而有争议的话题。典型的建议是尽量避免像这样筑巢,除非绝对必要

如果您确实需要以这种方式嵌套,那么您的代码中会出现类似以下情况的
update
变体:

键入ParentMsg
= ...
|ChildMsg Child.Msg
类型别名ParentModel=
{ ...
,child:child.Model
}
味精案例
ChildMsg ChildMsg->
let(childModel,childCmd)=Child.update childMsg model.Child
在{model | child=childModel}![Cmd.map ChildMsg childCmd]
在每一层嵌套中,您都需要连接子状态并来回传递子Msg和Cmds

更复杂的是,在代码中,您处理的是一个参与者列表,这意味着您还需要传递一个索引,以便知道您正在编辑列表中的哪个参与者()


好消息是,Elm使重构变得轻而易举!

当我把你的
Contributor.Elm
文件去掉几个函数使其编译时,
onClick
函数正在发送预期的消息。你有其他包装代码或覆盖可能会干扰吗?我不这么认为……我观察到了代码工作还有你的例子。有什么工具我可以用来诊断这个问题吗?我很感谢你的反馈。看起来我必须从两个极端中选择一个。(1):为嵌套控件编写额外的管道代码,同时保持每个模块文件的合理位置。或者(2):有一个膨胀的页面(即Home.elm)它具有用户可以导航到的每个页面的完整视图定义,所有这些都在这个模块中。