更新时间:2023-01-12 10:50:09
已更新为Elm 0.18
要公开Comments.Model
,请确保您的Comment.elm文件公开所有类型和功能,如下所示:
To expose Comments.Model
, make sure your Comments.elm file exposes all types and functions like this:
module Comments exposing (..)
或者,您可以公开如下类型和函数的子集:
Or, you can expose a subset of types and functions like this:
module Comments exposing (Model)
解码器存在一些问题.首先,要匹配您的JSON,您将需要一个记录类型别名,该别名公开一个posts
列表帖子.
There are a few problems with your decoders. First off, to match your JSON, you're going to need a record type alias that exposes a single posts
list Posts.
type alias PostListContainerModel =
{ posts : List Post.Model }
您没有评论用的解码器.看起来像这样:
You are missing a decoder for comments. That will look like this:
commentDecoder : Decoder Comment.Model
commentDecoder =
Decode.map2
Comment.Model
(Decode.field "text" Decode.string)
(Decode.field "date" Decode.string)
为了避免歧义,我将把您的decoder
函数重命名为postDecoder
.现在,您可以使用新的commentDecoder
修复comments
解码器行.
I'm going to rename your decoder
function to postDecoder
to avoid ambiguity. Now you can fix the comments
decoder line by using the new commentDecoder
.
postDecoder : Decoder Post.Model
postDecoder =
Decode.map5
Post.Model
(Decode.field "img" Decode.string)
(Decode.field "text" Decode.string)
(Decode.field "source" Decode.string)
(Decode.field "date" Decode.string)
(Decode.field "comments" (Decode.list commentDecoder))
最后,我们可以使用先前创建的帖子包装类型(PostListContainerModel
)来修复decoderColl
:
Lastly, we can fix decoderColl
by using that posts wrapper type (PostListContainerModel
) we created earlier:
decoderColl : Decoder PostListContainerModel
decoderColl =
Decode.map
PostListContainerModel
(Decode.field "posts" (Decode.list postDecoder))