首页 > 解决方案 > 模块导入错误

问题描述

在尝试导入 Date 模块并在一个简单的 Elm 文件中使用它时,我收到以下错误:

-- UNKNOWN IMPORT ---------------------------- app/javascript/CountdownTimer.elm

The CountdownTimer module has a bad import:

    import Date

I cannot find that module! Is there a typo in the module name?

The "source-directories" field of your elm.json tells me to only look in the src
directory, but it is not there. Maybe it is in a package that is not installed
yet?

CountdownTimer 是文件和模块的名称,取自此处,似乎工作正常,但不适合我。

我正在使用 Elm 0.19.0 和 Rails 6.0.0beta1,这似乎不是一个约束,因为如果我在任何地方进入 Elm REPL 并尝试导入日期,我将面临同样的错误:

> import Date
-- UNKNOWN IMPORT ---------------------------------------------------------- elm

The Elm_Repl module has a bad import:

    import Date

I cannot find that module! Is there a typo in the module name?

When creating a package, all modules must live in the src/ directory.

我的 elm.json 文件如下所示:

{
    "type": "application",
    "source-directories": [
        "src"
    ],
    "elm-version": "0.19.0",
    "dependencies": {
        "direct": {
            "elm/browser": "1.0.1",
            "elm/core": "1.0.2",
            "elm/html": "1.0.0",
            "elm/time": "1.0.0",
            "elm/json": "1.1.2",
            "elm/url": "1.0.0",
            "elm/virtual-dom": "1.0.2"
        },
        "indirect": {
        }
    },
    "test-dependencies": {
        "direct": {},
        "indirect": {}
    }
}

标签: dateelm

解决方案


您尝试使用的代码已过时。Date据我所知,已从coreElm 0.19 中删除,没有直接替换或涵盖完全相同功能的间接替换。elm/time旨在作为两者的官方继任者Dateand Time,但它不提供您在此处需要的日期/时间字符串解析。

Date.fromString的只是 JavaScript 解析 API 的一个薄包装器,因此不是很一致或定义良好。因此,为什么它被删除。在 Elm 0.19 中,您必须使用提供更具体功能的第三方包,例如elm-iso8601-date-strings

如果您收到的是 ISO 8601 字符串,您应该能够替换parseTime为:

import Iso8601
import Time

parseTime : String -> Time.Posix
parseTime string =
    Iso8601.toTime string
        |> Result.withDefault (Time.millisToPosix 0)

但是,如果您收到的不是 ISO 8601 格式,您需要找到另一个可以解析该特定格式的包。


推荐阅读