【问题标题】:Elm can't find the elm whitespace errorElm 找不到 elm whitespace 错误
【发布时间】:2018-04-05 11:52:17
【问题描述】:

我不断收到空白编译器错误,但我无法发现错误。 这是错误

我需要空格,但卡在看起来像新声明的地方。 你要么在上面的声明中遗漏了一些东西,要么只是 这里需要加一些空格:

41|视图:模型 -> Html -> 消息 ^ 我正在寻找以下内容之一:

whitespace

这里是代码

view : Model -> Html -> Msg
view model =
    div [] [
        h2 [] [text ("Counter: " ++ (toString model))]
        , button [type_ "button", onClick Add ] [text "add"]
        , button [type_ "button", onClick Sub ] [text "subtract"]
        , button [type_ "button", onClick Reset ] [text "reset"]
    ]

我必须遗漏一些非常简单的东西,但我无法发现它。

【问题讨论】:

  • 你能在view函数上面也包含代码吗?错误可能是上面的语句不完整,因此编译器在到达view 声明时会阻塞,但view 函数本身很好(除了Html Msg 类型错误)。

标签: compiler-errors whitespace elm


【解决方案1】:

函数调用后需要几个空格。喜欢

x =
0

不会编译,但是

x =
    0

会编译,这么说吧。你需要像这样缩进 div []

view : Model -> Html -> Msg
view model =
    div []
        [ h2 [] [ text ("Counter: " ++ (toString model)) ]
        , button [ type_ "button", onClick Add ] [ text "add" ]
        , button [ type_ "button", onClick Sub ] [ text "subtract" ]
        , button [ type_ "button", onClick Reset ] [ text "reset" ]
        ]

另外,视图的类型注释已关闭,目前您​​有

view : Model -> Html -> Msg

但应该是的

view : Model -> Html Msg

只是在学习时的提示,您可以省略类型别名

--view : Model -> Html Msg
view model =

很好,以后你对语言更熟悉的时候可以添加注释,我就是这么学的。

请参阅here 以获取工作版本的链接。

【讨论】:

  • 对不起,我在原来的 sn-p 中打错了。我是榆树新手,所以我没有注意到间距。以上是我产生错误的代码的样子。
【解决方案2】:

您的类型定义有点错误。以下:

view : Model -> Html -> Msg

应该是:

view : Model -> Html Msg

Html Msg 是单一类型。对应的类型定义为type alias Html msgmsg 以小写字母开头,表示此类型是泛型类型。这意味着任何类型都可以放在此处。例如,我们可以写成Html IntHtml String。例如,当我们单击按钮时,视图代码会返回消息,因此我们将此消息类型用作泛型类型。这导致Html Msg

【讨论】:

    猜你喜欢
    • 2017-03-29
    • 2018-04-14
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    相关资源
    最近更新 更多