如何在榆树中避免使用 HTTP 2.0.0 的 Expect 消息?
How to avoid Expect msg with HTTP 2.0.0 in elm?
在 elm 的 Http 1.0.0
包中,我可以发送自定义请求,例如:
post : Endoint -> List (Http.Header) -> Http.Body -> Decoder a -> Http.Request a
post url headers body decoder =
Http.request
{ method = "POST"
, headers = headers
, url = url
, body = body
, expect = Http.expectJson decoder
, timeout = Nothing
, withCredentials = False
}
用我上面写的post
函数,我可以简单地用一个Decoder String
调用它,在Http请求发送后,响应字符串将被解码并返回。时期。无需创建 Msg
,如:
type Msg
= GotText (Result Http.Error String)
并且无需在 update
中编写分支来处理此 Msg
。
但是,从 Http 2.0.0
开始,expect
参数的类型是 Expect msg
,而不是 Expect a
,这意味着编写 Msg
变体和附加的要更新的分支现在 需要 。
我正在编写一个 Api.elm
文件来发出 Http 请求。但是,这意味着现在它必须有自己的 Msg
类型和 update
函数才能在这些请求响应后 运行。
我曾经认为 Home.elm
应该只回复来自 Home.Msg
和 Home.update
的消息而不是 Api.Msg
和 Api.update
。我错了吗? Api.elm
是否应该有自己的 Msg
类型和 update
函数来更改其他页面?或者有更好的方法吗?
澄清我在问题中解释的内容:
Elm 的 HTTP 2.0.0 包中的自定义请求如下所示:
request :
{ method : String
, headers : List Header
, url = String
, body = Body
, expect = Expect msg
, timeout = Maybe Float
, withCredentials = Maybe String
}
-> Cmd msg
在 Http 1.0.0 中看起来像这样:
request :
{ method : String
, headers : List Header
, url : String
, body : Body
, expect : Expect a
, timeout : Maybe Float
, withCredentials : Bool
}
-> Request a
区别在于使用 HTTP 2.0.0 的自定义请求,我需要传入一个Msg
来使用这个请求。
现在,我的问题是:我使用的是 Api.elm
文件。每次我需要发出 HTTP 请求时,我都会从 Login.elm
.
调用 Api.request arg1 arg2...
由于 Api.elm
中的 request
函数需要 Msg
类型,在这种情况下,对于登录请求,我认为我必须定义 Msg
Api.elm
中的 GotLogin
,然后通过为 Api.elm
中的 GotLogin
编写 update
分支来处理 GotLogin
将如何更新 Login.elm
。
但是,我可以在 Login.elm
中定义一个 GotLogin
Msg
并将其传递给 Api.request
。由于我在 Login.elm
中定义了 GotLogin
,因此我会在 Login.elm
的更新函数中放置一个 GotLogin
分支,而不是 Api.elm
.
这也适用于来自任何其他页面的任何其他请求类型(Signup.elm
、Home.elm
、...),这意味着 Api.elm
不应有自己的 update
更新其他页面的函数。
Login.elm
拥有自己的 update
函数的全部意义在于,它应该只受其自身 update
函数分支的影响,而不是来自 [=13] 的分支=].
在 elm 的 Http 1.0.0
包中,我可以发送自定义请求,例如:
post : Endoint -> List (Http.Header) -> Http.Body -> Decoder a -> Http.Request a
post url headers body decoder =
Http.request
{ method = "POST"
, headers = headers
, url = url
, body = body
, expect = Http.expectJson decoder
, timeout = Nothing
, withCredentials = False
}
用我上面写的post
函数,我可以简单地用一个Decoder String
调用它,在Http请求发送后,响应字符串将被解码并返回。时期。无需创建 Msg
,如:
type Msg
= GotText (Result Http.Error String)
并且无需在 update
中编写分支来处理此 Msg
。
但是,从 Http 2.0.0
开始,expect
参数的类型是 Expect msg
,而不是 Expect a
,这意味着编写 Msg
变体和附加的要更新的分支现在 需要 。
我正在编写一个 Api.elm
文件来发出 Http 请求。但是,这意味着现在它必须有自己的 Msg
类型和 update
函数才能在这些请求响应后 运行。
我曾经认为 Home.elm
应该只回复来自 Home.Msg
和 Home.update
的消息而不是 Api.Msg
和 Api.update
。我错了吗? Api.elm
是否应该有自己的 Msg
类型和 update
函数来更改其他页面?或者有更好的方法吗?
澄清我在问题中解释的内容:
Elm 的 HTTP 2.0.0 包中的自定义请求如下所示:
request :
{ method : String
, headers : List Header
, url = String
, body = Body
, expect = Expect msg
, timeout = Maybe Float
, withCredentials = Maybe String
}
-> Cmd msg
在 Http 1.0.0 中看起来像这样:
request :
{ method : String
, headers : List Header
, url : String
, body : Body
, expect : Expect a
, timeout : Maybe Float
, withCredentials : Bool
}
-> Request a
区别在于使用 HTTP 2.0.0 的自定义请求,我需要传入一个Msg
来使用这个请求。
现在,我的问题是:我使用的是 Api.elm
文件。每次我需要发出 HTTP 请求时,我都会从 Login.elm
.
Api.request arg1 arg2...
由于 Api.elm
中的 request
函数需要 Msg
类型,在这种情况下,对于登录请求,我认为我必须定义 Msg
Api.elm
中的 GotLogin
,然后通过为 Api.elm
中的 GotLogin
编写 update
分支来处理 GotLogin
将如何更新 Login.elm
。
但是,我可以在 Login.elm
中定义一个 GotLogin
Msg
并将其传递给 Api.request
。由于我在 Login.elm
中定义了 GotLogin
,因此我会在 Login.elm
的更新函数中放置一个 GotLogin
分支,而不是 Api.elm
.
这也适用于来自任何其他页面的任何其他请求类型(Signup.elm
、Home.elm
、...),这意味着 Api.elm
不应有自己的 update
更新其他页面的函数。
Login.elm
拥有自己的 update
函数的全部意义在于,它应该只受其自身 update
函数分支的影响,而不是来自 [=13] 的分支=].