我的应用程序使用Navigate进行路由,但由于某种原因,初始http命令没有为其中一个子模块触发。在父模块和子模块之间连接命令的方式可能有问题,但我可以使用一些帮助来定位问题。代码和结构以elm-taco(https://github.com/ohanhi/elm-taco)为例进行建模。下面是代码相关部分的摘录,没有编译器错误。
Main.elm
init : Flags -> Location -> ( AppModel, Cmd Msg )
init flags location =
( startModel
, Http2.get ("localhost:4000/graphql?query=" ++ encoded) HandleLogDataResponse decoder)
update : Msg -> AppModel -> ( AppModel, Cmd Msg )
update msg model =
case msg of
HandleLogDataResponse webData ->
updateLogData model webData
RouterMsg routerMsg ->
updateRouter model routerMsg
updateLogData : AppModel -> WebData LogData -> ( AppModel, Cmd Msg )
updateLogData model webData =
case webData of
Success logdata ->
case model.appState of
NotReady time ->
let
initTaco =
{ currentTime = time
, logdata = logdata
}
( initRouterModel, routerCmd ) =
Router.init initTaco model.location
in
( { model | appState = Ready initTaco initRouterModel }
, Cmd.map RouterMsg routerCmd
)
Ready taco routerModel ->
( { model | appState = Ready (updateTaco taco (UpdateLogData logdata)) routerModel }
, Cmd.none
)
Router.elm
init : Taco -> Location -> (Model, Cmd Msg)
init taco location =
let
( chatModel, chatCmd) =
Chat.initModel taco
in
( { chatModel = chatModel
, route = parseLocation location}
, Cmd.map ChatMsg chatCmd
)
update: Msg -> Model -> ( Model,Cmd Msg, TacoUpdate)
update msg model =
case msg of
ChatMsg chatMsg ->
updateChat model chatMsg
updateChat : Model -> Chat.Msg -> (Model, Cmd Msg, TacoUpdate)
updateChat model chatMsg =
let
( nextChatModel, chatCmd, tacoUpdate) =
Chat.update chatMsg model.chatModel
in
( {model | chatModel = nextChatModel }
, Cmd.map ChatMsg chatCmd
, tacoUpdate)
Chat.elm
initModel : Taco -> ( Model, Cmd Msg )
initModel taco =
let
startModel = { newMessage = ""
, messages = taco.messages
, response = "Waiting for a response..."
, logs = []
}
cmd = Http.send FetchHNTopStories request
in
( startModel
, cmd
)
答案 0 :(得分:2)
为了获得执行命令的结果,您需要为它提供一种到达运行时的方法。
这是通过main
完成的。如果您的Cmd
未访问该应用的main
,则永远不会执行这些内容。
在您提供的代码示例中,子模块init
如何到达main
并不明显。传统上,init
中的Main.elm
应使用子模块中的init
,Cmd.map
子模块init Cmds和Cmd.batch
以及其余部分主要init
Cmds。