如何 return Elm 中的元素列表
How to return a list of elements in Elm
我正在尝试从 ELM 中的简单数组构建元素列表。预期结果实际上只是一个元素列表,第一项为 1,第二项为 2,依此类推。
import Html exposing (..)
import Html.Attributes exposing (class, id)
import List exposing (map)
theArray = [1,2,3,4,5,6]
createListItem item =
li [] [ text (toString item)]
buildList collection =
map createListItem collection
builtList = ul [] [(buildList theArray)]
main =
builtList
但是我一直在第十三行收到编译器错误。我已经尝试将地图元素类型注释为 html,但我不知道我应该做什么。
The 2nd argument to function `ul` is causing a mismatch.
*13| builtList = ul [] [(buildList theArray)]*
Function `ul` is expecting the 2nd argument to be:
List VirtualDom.Node
But it is:
List (List Html)
buildList
已经返回类型 List Html
的值,因此您不需要用方括号括起 (buildList theArray)
。将第 13 行更改为:
builtList = ul [] (buildList theArray)
我正在尝试从 ELM 中的简单数组构建元素列表。预期结果实际上只是一个元素列表,第一项为 1,第二项为 2,依此类推。
import Html exposing (..)
import Html.Attributes exposing (class, id)
import List exposing (map)
theArray = [1,2,3,4,5,6]
createListItem item =
li [] [ text (toString item)]
buildList collection =
map createListItem collection
builtList = ul [] [(buildList theArray)]
main =
builtList
但是我一直在第十三行收到编译器错误。我已经尝试将地图元素类型注释为 html,但我不知道我应该做什么。
The 2nd argument to function `ul` is causing a mismatch.
*13| builtList = ul [] [(buildList theArray)]*
Function `ul` is expecting the 2nd argument to be:
List VirtualDom.Node
But it is:
List (List Html)
buildList
已经返回类型 List Html
的值,因此您不需要用方括号括起 (buildList theArray)
。将第 13 行更改为:
builtList = ul [] (buildList theArray)