1
我的應用程序通過標誌從localstorage獲取init模型值。我在模型中添加了一個新的鍵,並且由於通過標誌傳遞的值中缺少鍵(「bar」),所以在啓動Elm應用程序時會導致錯誤。考慮到將來可以添加更多的新密鑰,並且我不希望每次發生時都要清除本地存儲,有沒有辦法告訴Elm在標記中缺少密鑰時分配默認值?在Elm中正常處理標記中缺失的鍵
type alias Model =
{ foo : String, bar : Int }
update : msg -> Model -> (Model, Cmd msg)
update _ model =
model ! []
view : Model -> Html msg
view model =
text <| toString model
main : Program Flags Model msg
main =
Html.programWithFlags
{ init = init
, update = update
, view = view
, subscriptions = always Sub.none
}
HTML代碼
<body>
<script>
var app = Elm.Main.fullscreen({foo: "abc"})
</script>
</body>