Http 我想在Elm中初始化函数
我正在尝试将flags值从Main.elm传递到Top.elm 并运行一个名为“getToken”的函数,该函数具有标志值。 但是,这个函数“getToken”似乎不能正常工作 我的主要朋友埃尔姆是Http 我想在Elm中初始化函数,http,elm,Http,Elm,我正在尝试将flags值从Main.elm传递到Top.elm 并运行一个名为“getToken”的函数,该函数具有标志值。 但是,这个函数“getToken”似乎不能正常工作 我的主要朋友埃尔姆是 type alias Model = { key : Nav.Key , page : Page } type Page = NotFound | TopPage ( Page.Top.Model, Cmd Page.Top.Msg ) | Art
type alias Model =
{ key : Nav.Key
, page : Page
}
type Page
= NotFound
| TopPage ( Page.Top.Model, Cmd Page.Top.Msg )
| ArticlePage Page.Article.Model
init : String -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
init flags url key =
Model key (TopPage (Page.Top.init flags))
|> goTo (Route.parse url)
type alias Model =
{ status : LoadingStatus
}
type LoadingStatus
= Init
| Waiting
| Loaded Token
| Failed Http.Error
init : String -> ( Model, Cmd Msg )
init token =
( Model Init
, getToken token )
...
-- UPDATE
type Msg
= GotToken (Result Http.Error Token)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
GotToken (Ok token) ->
( { model | status = Loaded token }, Cmd.none )
GotToken (Err err) ->
( { model | status = Failed err }, Cmd.none )
-- VIEW
view : Model -> Html Msg
view model =
case model.status of
Init ->
text "sssssssssssssssssssssssssssss"
Waiting ->
text "waiting..."
Loaded token ->
div []
[ p [] [ text "yahoooooooo" ]
, p [] [ text token.accessToken ]
]
Failed e ->
case e of
Http.BadBody message ->
pre [] [ text message ]
_ ->
-- text (Debug.toString e)
pre [] [ text "error" ]
...
type alias Token =
{ accessToken : String
}
-- AccessToken
getToken : String -> Cmd Msg
getToken token =
Http.request
{ method = "POST"
, headers =
[ Http.header "Authorization" ("Bearer " ++ token)
, Http.header "Accept" "application/json"
, Http.header "Content-Type" "application/json"
]
, url = "http://localhost:8080/web_users/create"
, expect = Http.expectJson GotToken tokenDecoder
, body = Http.emptyBody
, timeout = Nothing
, tracker = Nothing
}
tokenDecoder : Decoder Token
tokenDecoder =
D.map Token
(D.field "access_token" D.string)
我的上衣,埃尔姆是
type alias Model =
{ key : Nav.Key
, page : Page
}
type Page
= NotFound
| TopPage ( Page.Top.Model, Cmd Page.Top.Msg )
| ArticlePage Page.Article.Model
init : String -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
init flags url key =
Model key (TopPage (Page.Top.init flags))
|> goTo (Route.parse url)
type alias Model =
{ status : LoadingStatus
}
type LoadingStatus
= Init
| Waiting
| Loaded Token
| Failed Http.Error
init : String -> ( Model, Cmd Msg )
init token =
( Model Init
, getToken token )
...
-- UPDATE
type Msg
= GotToken (Result Http.Error Token)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
GotToken (Ok token) ->
( { model | status = Loaded token }, Cmd.none )
GotToken (Err err) ->
( { model | status = Failed err }, Cmd.none )
-- VIEW
view : Model -> Html Msg
view model =
case model.status of
Init ->
text "sssssssssssssssssssssssssssss"
Waiting ->
text "waiting..."
Loaded token ->
div []
[ p [] [ text "yahoooooooo" ]
, p [] [ text token.accessToken ]
]
Failed e ->
case e of
Http.BadBody message ->
pre [] [ text message ]
_ ->
-- text (Debug.toString e)
pre [] [ text "error" ]
...
type alias Token =
{ accessToken : String
}
-- AccessToken
getToken : String -> Cmd Msg
getToken token =
Http.request
{ method = "POST"
, headers =
[ Http.header "Authorization" ("Bearer " ++ token)
, Http.header "Accept" "application/json"
, Http.header "Content-Type" "application/json"
]
, url = "http://localhost:8080/web_users/create"
, expect = Http.expectJson GotToken tokenDecoder
, body = Http.emptyBody
, timeout = Nothing
, tracker = Nothing
}
tokenDecoder : Decoder Token
tokenDecoder =
D.map Token
(D.field "access_token" D.string)
服务器启动后,可以在视图中看到文本“SSSSSSSSS”。
看来Top.elm中的Init正在工作。
但是,它不会走得更远
我假设getToken函数在这里不起作用,因为我只能看到LoadingStatus Init(未等待、已加载、已失败)
有人知道为什么getToken函数不能正常工作吗
没有发生错误。最有可能的是,来自
Top.init的命令没有到达运行时
我看到您编码了TopPage(Page.Top.Model,Cmd Page.Top.Msg)
。这很可能是罪魁祸首。您需要类似于TopPage Page.Top.Model
的内容,并确保Top.init
返回的命令与Main.init
它看起来是这样的:
init : String -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
init flags url key =
let
( topModel, topCmd ) =
Page.Top.init flags
( mainModel, mainCmd ) =
Model key (TopPage topModel)
|> goTo (Route.parse url)
in
( mainModel, Cmd.batch [ mainCmd, topCmd ] )
最有可能的是,来自Top.init
的命令没有到达运行时
我看到您编码了TopPage(Page.Top.Model,Cmd Page.Top.Msg)
。这很可能是罪魁祸首。您需要类似于TopPage Page.Top.Model
的内容,并确保Top.init
返回的命令与Main.init
它看起来是这样的:
init : String -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
init flags url key =
let
( topModel, topCmd ) =
Page.Top.init flags
( mainModel, mainCmd ) =
Model key (TopPage topModel)
|> goTo (Route.parse url)
in
( mainModel, Cmd.batch [ mainCmd, topCmd ] )
也许服务器挂起了?浏览器的调试器对此有何评论?谢谢!实际上它还没有到达服务器。浏览器调试器什么也没说。你能提供一个吗?看看开发控制台的网络选项卡服务器挂起的原因吗?浏览器的调试器对此有何评论?谢谢!实际上它还没有到达服务器。浏览器调试器什么也没说。你能提供一个吗?看看开发控制台的网络选项卡