2
我將通過端口的對象數組傳遞給我的Elm應用程序。數組中的對象的一個的一個例子是:通過Elm端口將對象導入JSON解碼器
{
FullName: 'Foo Bar',
Location: 'Here'
}
正如你所看到的物體的按鍵用大寫字母開頭,所以我需要在榆樹解碼這些。在我的榆樹的代碼我對Person
type alias Person =
{ fullName : String
, location : String
}
和端口type
:
port getPeople : (List Json.Decode.Value -> msg) -> Sub msg
最後我有一個解碼器(我使用Elm Decode Pipeline)將數據解析到Person
類型。
peopleDecoder : Decoder Person
peopleDecoder =
decode Person
|> required "FullName" string
|> required "Location" string
我的問題是我怎麼輸入端口的數據映射到Person
類型?我知道我可以在JS中這樣做,但我寧願在我的Elm代碼中執行此操作。
在您的示例'peopleDecoder'中,什麼是'Workerer'? –
這是一個錯字,現在修復。 – joshuahornby10