如何从 elm 对​​象获取特定的键值对

问题描述

我有一个 elm 对​​象,它的返回值如下

type alias Info =
    { name : String,stdId : stdKey,pemId : PermanentKey
    }

此信息位于名为 class.elm 的页面

在另一个页面中,我想单独使用 std 键进行 if else 比较。 我绑定将 stdKey 分配给一个变量,如下所示

     uniKey = class.Info.stdId

但榆树不接受这种方式。 请帮忙。

解决方法

假设您打算在 Class.elm 文件中创建一个 Info 值并在 Other.elm 文件中使用它:

文件类.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"
   } 



文件其他.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