Elm中不正确的View Functon返回类型 [英] Incorrect View Functon Return Type in elm
问题描述
我正在制作一个程序,将屏幕上的渲染文本更改为用户在文本框中输入的内容。我认为我的Elm体系结构模型和更新部分正确无误,但我真的不理解视图部分。
I am making a program that changes the rendered text on the screen to be whatever the user inputs in a text box. I think I have the model and the update part of the elm architecture correct, but I really don't understand the view portion.
我只是在把头包裹在方括号视图功能上时遇到麻烦。
I'm just having trouble wrapping my head around the square bracket view functions.
无论如何,我遇到此错误。
Anyway, I am getting this error.
此 div
调用产生:
Html #(Model -> Model)#
但是类型注释在视图
上说应该是:
But the type annotation on view
says it should be:
Html #Msg#Elm
但是我不确定如何更改视图函数以返回Html Msg,我有点困惑和一个字符串之间的区别。
But I am not sure how to change my view function to return Html Msg and I am kinda confused between the difference between that and a string.
谢谢大家!
这是我的代码...
module Main exposing (..)
import Browser
import Html exposing (Html, div, text, input, Attribute)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput)
main =
Browser.sandbox { init = init, update = update, view = view }
type alias Model = String
init : Model
init = "Hello, World!"
type alias Msg = String
update : Msg -> Model -> Model
update msg model =
msg
view : Model -> Html Msg
view model =
div []
[ input [ placeholder "Input new string", value model, onInput update ] []
, div [] [ text model ]
]
推荐答案
您正在通过 update
作为onInput的参数。您可能打算向其传递Msg,然后运行时将其传递给更新函数。
You're passing the update
function as an argument to onInput. Your probably meant to pass it a Msg, which the runtime will then pass to the update function.
由于您的Msg类型是String的别名,因此可以使用 onInput身份
Since your Msg type is an alias for String, you can use onInput identity
这篇关于Elm中不正确的View Functon返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!