我是 Purescript 的新手,正在尝试学习 Halogen/Aff。我一直在开发一个简单的应用程序,它是 purescript-halogen 存储库中 effects-aff-ajax 示例的变体。我几乎可以正常工作,但出现以下错误。
得到错误的代码部分与 effects-aff-ajax 示例中的代码非常相似(我没有编译,但我只能假设有效)。检查代码我看不出它不能解除 Aff 响应的原因。
Error found:
in module Component
at src/purs/Component.purs:59:17 - 59:26 (line 59, column 17 - line 59, column 26)
No type class instance was found for
Effect.Aff.Class.MonadAff m2
while checking that type forall m. MonadAff m => (forall a. Aff a -> m a)
is at least as general as type t0 -> t1
while checking that expression liftAff
has type t0 -> t1
in value declaration handleAction
where m2 is a rigid type variable
bound at (line 54, column 16 - line 60, column 60)
t1 is an unknown type
t0 is an unknown type
这是完整的模块:
module Component (component) where
import Prelude (Unit, bind, discard, map, ($), (<<<), (<>))
import Affjax as AX
import Affjax.RequestBody as AXRB
import Affjax.ResponseFormat as AXRF
import Data.Either (hush)
import Data.Maybe (Maybe(..))
import Effect.Class (class MonadEffect)
import Halogen as H
import Halogen.HTML as HH
import Halogen.HTML.Events as HE
import Halogen.HTML.Properties as HP
import Web.Event.Event as E
type State = { source :: String, translation :: Maybe String }
data Action = InputSource String
| TranslateSource State E.Event
component :: ∀ f i o m. MonadEffect m => H.Component HH.HTML f i o m
component =
H.mkComponent
{ initialState
, render
, eval: H.mkEval $ H.defaultEval { handleAction = handleAction }
}
initialState :: ∀ i. i -> State
initialState _ = { source: "that wine is very delicious", translation: Nothing }
render :: ∀ m. State -> H.ComponentHTML Action () m
render state =
HH.form
[ HE.onSubmit (Just <<< TranslateSource state) ]
[ HH.h1_ [ HH.text "English to Italian Translation" ]
, HH.input
[ HP.type_ HP.InputText
, HP.value state.source
, HE.onValueInput $ Just <<< InputSource
]
, HH.p_ []
, HH.button
[ HP.type_ HP.ButtonSubmit ]
[ HH.text "Translate" ]
, HH.p_ [ HH.text $ case state.translation of
Nothing -> ""
Just t -> "Translation: " <> t
]
]
handleAction :: ∀ o m. MonadEffect m => Action -> H.HalogenM State Action () o m Unit
handleAction = case _ of
InputSource s ->
H.modify_ _{ source = s }
TranslateSource st ev -> do
H.liftEffect $ E.preventDefault ev
response <- H.liftAff $ AX.post AXRF.string "http://locahost:8080/translate" (Just $ AXRB.string st.source)
H.modify_ _{ translation = map _.body (hush response) }