【发布时间】:2016-12-23 19:42:59
【问题描述】:
我正在尝试使用端口将 URL 传递给 Javascript,以便将用户重定向到另一个页面。我写了一个port module 来包含我的项目所需的所有端口:
port module Utils exposing (..)
port changePage : String -> Cmd Event
然后,我将它导入到我的 Elm 文件中:
type Event = PageChange String
import Utils exposing (changePage)
但编译器不喜欢它:
It looks like the keyword `import` is being used as a variable.
8| import Utils exposing (changePage)
^
Rename it to something else.
所以我将端口定义移动到主文件:
type Event = PageChange String
port changePage : String -> Cmd Event
但是编译器还是不同意:
Port `changePage` has an invalid type.
28| port changePage : String -> Cmd Event
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You are saying it should be:
String -> Platform.Cmd.Cmd BmC.Index.Event
But you need to use the particular format described here:
<http://guide.elm-lang.org/interop/javascript.html#ports>
所以我去看了那个特殊的格式,port check : String -> Cmd msg。我不明白msg是从哪里来的,所以我去查了code,还是不明白那行是什么意思。
msg 来自哪里? type Cmd msg = Cmd 是什么意思?提前致谢。
【问题讨论】:
-
看看example 我为类似的问题所做的。在出端口
Cmd msg类型只表示它是一个命令,因为出端口不发送任何消息,你应该省略消息。 -
谢谢!它解决了我的问题。但没有回答我的问题。
标签: elm elm-architecture