榆树:如何将Html FooMsg转换为Html Msg

时间:2017-08-08 03:37:02

标签: elm

我是榆树的新手。我有一个有两页的网站:主页和注册。

Home拥有自己的视图,Signup拥有自己的视图,但它们都返回Html Msg。我想更改它以便Home返回Html HomeMsg并且Signup返回Html SignupMsg

编写这些视图函数当然很容易,但我认为我的顶级视图函数需要将结果转换为Html Msg

这是Msg类型。

type Msg 
  = Home HomeMsg
  | Signup SignupMsg
  | OnLocationChange Location

我认为我需要某种map函数来执行此操作,如

view : Model -> Html Msg 
view model =
    case model.route of
        Model.HomeRoute ->
            map Home (homeView model)

        Model.SignupRoute ->
            map Signup (signupView model)

        Model.NotFoundRoute ->
            notFoundView

1 个答案:

答案 0 :(得分:4)

是的,有一个map功能。它属于Html模块。

http://package.elm-lang.org/packages/elm-lang/html/2.0.0/Html#map

您的代码变为:

view : Model -> Html Msg 
view model =
    case model.route of
        Model.HomeRoute ->
            Html.map Home (homeView model)

        Model.SignupRoute ->
            Html.map Signup (signupView model)

        Model.NotFoundRoute ->
            notFoundView