榆树包源码

Elm package source code

我想查看在我的项目中使用的 elm-lang/core 的源代码。

我的项目有:

import Json.Decode exposing (..)

现在 elm 编译器说

Cannot find variable `Json.Decode.Decoder`.
`Json.Decode` does not expose `Decoder`. 

github source 可以看出它正在暴露 Decoder。想看看是不是我的 Elm 版本不对之类的。

以防万一-我的榆树-package.json有

"dependencies": {...
    "elm-lang/core": "5.1.1 <= v < 6.0.0",
     ...
},
"elm-version": "0.18.0 <= v < 0.19.0"

您在评论中使用的示例表明您正在使用 Decoder,如下所示:

on "load" (Decode.Decoder (toString model.gifUrl)

这确实是一个编译器错误。虽然 Json.Decode 包公开了 Decoder type,但它没有公开 Decoder constructor。这被称为不透明类型,这意味着您不能自己构造 Decoder 值,而只能使用 Json.Decode 包中的函数。不透明类型可以通过这样定义的模块来公开:

module Foo exposing (MyOpaqueType)

您可以通过以下方式之一指定公开哪些构造函数:

-- only exposes Constructor1 and Constructor2
module Foo exposing (MyType(Constructor1, Constructor2))

-- exposes all constructors of MyType
module Foo exposing (MyType(..))

根据您的示例代码,我推断您希望在图像完全加载时发生一些 Msg。如果是这样的话,那么你可以使用这样的东西:

type Msg
    = ...
    | ImageLoaded String

viewImage model =
    img [ src model.gifUrl, on "load" (Json.Decode.succeed (ImageLoaded model.gifUrl)) ] []

Here is an example of how to handle both the image load and error events.