如何从 Elm 中的 Html.Attribute 中提取值

How to extract value from Html.Attribute in Elm

我想用 Elm 语言阅读 Html.Attribute 时遇到问题。 我想制作一个如下所示的自定义标签;

myTag : List ( Html.Attribute msg ) -> List ( Html msg ) -> Html msg
myTag attributes children = 
    -- case attributes has "src" show button with image
    -- otherwise show normal button

所以当我调用 myTag 时,如果我指定 src,我将自动获得带有图像的按钮,例如;

myTag [ src="somepic.jpg" ][]  -- show a button with image

myTag [][]                     -- show a normal button

我试着调试看看里面有什么"Html.Attribute"。它包含类似 ( a2 "<tag-name>" <tag-value> )( a3 "<tag-name>" <tag-value> ) 的内容。但是,我不知道如何提取 "<tag-name>",我认为它是来自这些值的字符串

Html.Attribute 类型是不透明的。没有任何信息可以从中提取出来用于 Elm 应用程序,因为该包不提供从 Elm 代码中提取数据或检查 Attribute 值的方法。

是的,您 可以 使用 javascript 调试器来查看 Elm 代码如何编译成 Javascript,但这对从 Elm 代码中解构 Attribute 类型。

如果您想检查属性,我建议您改用 Dict String String 作为您的属性参数:

myTag : Dict String String -> List ( Html msg ) -> Html msg