Elm:端口不能在嵌套模块中工作

时间:2017-11-16 13:28:11

标签: javascript nested elm ports

我正在尝试通过调整嵌套的工作示例来使端口在嵌套模块中工作。在这种情况下,Save2.elm调用一个JavaScript函数fileSelected。 但是,这不起作用。

调试显示消息“Json Selected”被触发。不幸的是,JavaScript函数永远不会被调用。 Ports.elm中定义的端口:

port fileSelected : String -> Cmd msg

Save2.elm的相关部分,“Json Selected”部分被解雇:

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        JsonSelected inputBoxId ->
            ( model
            , fileSelected inputBoxId
            )

相关的JavaScript部分:

<script src="main.js"></script>
<script>
    var app = Elm.Main.fullscreen();
    // This log is shown from start-up
    console.log("Upload.html console.log is shown")
    app.ports.fileSelected.subscribe(function (id) {
      // This log is never shown
      console.log("fileSelected is executed")

回购的链接: https://github.com/gitLabor8/Elm-Ports-not-working-in-nested-modules

提前致谢,

1 个答案:

答案 0 :(得分:0)

在您的回购中,Cmd中的Save2.update被忽略{/ 1}}。

Main.update

您需要从update : Msg -> Model -> ( Model, Cmd Msg ) update msg model = case msg of Save2 saveMsg -> ( { model | save2 = Tuple.first (Save2.update saveMsg model.save2) } , Cmd.none ) 返回它,以便Elm运行时可以处理Main.update

Cmd

在Elm中,调用update : Msg -> Model -> ( Model, Cmd Msg ) update msg model = case msg of Save2 saveMsg -> let ( save2Model, save2Cmd ) = Save2.update saveMsg model.save2 in ( { model | save2 = save2Model } , save2Cmd ) 函数不会触发任何操作。他们只返回必须由Elm运行时处理的port