初學者計劃

Html 主要用於學習目的。

beginnerProgram 無法處理訂閱或執行命令。

它只能處理來自 DOM Events 的使用者輸入。

它只需要 view 來渲染 model 和 update 函式來處理狀態變化。

例

考慮一下 beginnerProgram 這個最小的例子。

本例中的 model 由單個 Int 值組成。

update 函式只有一個分支,它增加了儲存在 model 中的 Int。

view 呈現模型並附加單擊 DOM 事件。

瞭解如何在 Initialize 和 build 中構建示例

import Html exposing (Html, button, text)
import Html exposing (beginnerProgram)
import Html.Events exposing (onClick)

main : Program Never
main =
    beginnerProgram { model = 0, view = view, update = update }

-- UPDATE

type Msg
    = Increment

update : Msg -> Int -> Int
update msg model =
    case msg of
        Increment ->
            model + 1

-- VIEW

view : Int -> Html Msg
view model =
    button [ onClick Increment ] [ text ("Increment: " ++ (toString model)) ]