【发布时间】:2018-02-20 22:58:14
【问题描述】:
是否可以使用==比较Elm中标记联合类型的构造函数,还是必须用例?
例子:
type alias Model =
{ accessToken : String
, page : Page
, config : Config
}
type Page
= Home String
| Profile String
menu : Model -> Html Msg
menu model =
div []
[ a [ href "#home", classList [ ("active", model.page == Home) ] ][ text "Home" ]
, a [ href "#profile", classList [ ("active", model.page == Profile)] ][ text "Profile" ]
]
在示例中,我想编写类似 model.page == Home 的内容来检查当前页面是否为 Home,以便我可以在该链接上将 css 类设置为“活动”,但似乎我必须为此使用一个案例,我可以这样做,但对于这种情况实施有点尴尬。
【问题讨论】:
标签: elm union-types