elm:定义没有参数的订阅端口

Bud*_*ril 7 elm elm-port

有没有办法在Elm中定义没有参数的订阅端口?

就像是:

port updateTime : () -> Sub msg
Run Code Online (Sandbox Code Playgroud)

使用此代码,我收到"端口'updateTime'具有无效类型"的错误

随着代码:

port updateTime : (String -> msg) -> Sub msg
Run Code Online (Sandbox Code Playgroud)

它正在工作,但我不需要从javascript函数向Elm发送任何内容.

Cha*_*ert 12

你可以得到的最接近的可能是这样的:创建类似于你的例子的端口,但第一个参数为(() -> msg):

port updateTime : (() -> msg) -> Sub msg
Run Code Online (Sandbox Code Playgroud)

假设你有一个UpdateTime Msg不接受任何参数,

type Msg
    = ...
    | UpdateTime
Run Code Online (Sandbox Code Playgroud)

然后你可以updateTime像这样绑定订阅:

subscriptions : Model -> Sub Msg
subscriptions model =
    updateTime (always UpdateTime)
Run Code Online (Sandbox Code Playgroud)

现在,在javascript方面,你必须null作为唯一的参数传递.

app.ports.updateTime.send(null);
Run Code Online (Sandbox Code Playgroud)