从子页面路由到另一个页面

时间:2016-10-31 14:13:25

标签: elm

我目前正在使用Hop,我想知道如何路由到main / app模块之外的其他页面。如果您按照Hop文档进行操作,则会有两种特定类型的消息 - 在文档中称为NavigateToSetQuery。你如何从子模块中提取这些消息?

我尝试了以下内容:

view =
    button [ onClick (Main.Types.NavigateTo "test") ] []

然而,这会搞砸打字。

1 个答案:

答案 0 :(得分:1)

问题不在于Hop,而是我对父母在Qm中如何运作的理解。轻描淡写地说,你需要注意什么以及什么时候使用这种类型的通信,但就我而言,我在Brian ThicksAlex Lew上阅读了一些好的博客文章,谈论这种形式的沟通。特别是Alex'帖子对我的用例来说很典型。

我所做的是以下内容:我为我想要路由的消息类型添加了一个单独的更新语句。这不是最好的实现,它可以像Alex用翻译模式更优雅地完成。

update msg model =
    case msg of
        NavigateTo path ->
            let
                command =
                    Hop.outputFromPath hopConfig path
                        |> Navigation.newUrl
            in
                ( model, command )

        SetQuery query ->
            let
                command =
                    model.address
                        |> Hop.setQuery query
                        |> Hop.output hopConfig
                        |> Navigation.newUrl
            in
                ( model, command )

        ExampleMsg InterestingMsg exampleInteger ->
            update (NavigateTo "<path here>") model --Update model in let-statement

        ExampleMsg subMsg ->
            let
                ( updatedExampleModel, pageCmd ) =
                    Page.Example.State.update subMsg model.exampleModel
            in
                ( { model | exampleModel = updatedExampleModel }, Cmd.map ExampleMsg pageCmd )