如何从elm对象获取特定的键值对
我有一个elm对象,它返回的值如下如何从elm对象获取特定的键值对,elm,Elm,我有一个elm对象,它返回的值如下 type alias Info = { name : String , stdId : stdKey , pemId : PermanentKey } uniKey = class.Info.stdId 此信息位于名为class.elm的页面中 在另一个页面中,我想单独使用std键进行if-else比较。 我将stdKey分配给一个变量,如下所示 type alias Info = { name : Str
type alias Info =
{ name : String
, stdId : stdKey
, pemId : PermanentKey
}
uniKey = class.Info.stdId
此信息位于名为class.elm的页面中
在另一个页面中,我想单独使用std键进行if-else比较。
我将stdKey分配给一个变量,如下所示
type alias Info =
{ name : String
, stdId : stdKey
, pemId : PermanentKey
}
uniKey = class.Info.stdId
但是榆树不接受这种方式。
请提供帮助。假设您打算在Class.elm文件中创建信息值并在其他.elm文件中使用它: 文件Class.elm
module Class exposing (Info)
type alias Info =
{ name : String
, stdId : StdKey
, pemId : PermanentKey
}
type alias StdKey = String
type alias PermanentKey = String
info : Info
info =
{ name = "name"
, stdKey = "valueForStdKey"
, pemKey = "valueForPemKey"
}
文件Other.elm
module Other exposing (..)
import Class
uniKey : Class.StdKey
uniKey = Class.info.stdKey
另类
文件Alt.elm
module Alt exposing (..)
import Class exposing (StdKey, PermanentKey, info)
uniKey : StdKey
uniKey = info.stdKey
Elm会自动生成一个
.stdId
函数,该函数适用于具有该字段的任何记录
.stdId info == info.stdId
type alias Info
声明不创建变量,而是定义自定义类型stdKey
应该是一个类型,因此应该是stdKey
,您必须为其提供类型定义。例如,type alias StdKey=String
我想知道Elm给你的错误消息是什么。type alias StdKey=String type alias PermanentKey=String都来自不同的页面,我可以像你说的那样导入它吗。是的。所有值和类型都可以通过导入定义它们的模块来共享。如果使用exposing
,则不必在值或类型前面加上导入模块的名称。