Elm:如何创建不返回Msg的视图? [英] Elm: How to create a view that doesn't return Msg?

查看:43
本文介绍了Elm:如何创建不返回Msg的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我有一个仅显示错误文本的嵌套组件。 view方法接收 Model 并返回 Html ,但是编译器抱怨说Html需要另一个值,所以我结束了这样做:

Currently, I have a nested component that only shows an error text. The view method receives a Model and returns Html, but the compiler complains saying that Html needs another value, so I ended up doing this:

view: Model -> Html ()
view error =
    div [class "docs-section error-bar"] [
      errorText error
    ]

errorText: Model -> Html ()
errorText error =
  case error.text of
    Nothing -> span [][]
    Just value -> text value

我不喜欢的是我必须添加 ()签名的值。

What I don't like is that I have to add the () value to the signature. Is there a way I can get rid of this?

谢谢!

推荐答案

Html 类型仅需要一个参数,因此您总是需要提供 something 。使用()作为类型参数是在其中放置虚拟值的常用方法。

The Html type requires exactly one parameter, so you always have to give is something. Using () as a type parameter is a common way to put a dummy value there.

您始终可以定义类型别名以避免每次输入()

You could always define a type alias to avoid typing () every time:

type alias OnlyHtml = Html ()

然后您可以适当地更改类型签名:

Then you could change your type signatures appropriately:

view: Model -> OnlyHtml

这篇关于Elm:如何创建不返回Msg的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆