- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个模式窗口,可以在其中显示不同的组件。每个组件都有自己的更新程序和消息,但我想在它们之间共享一个关闭按钮。
因此我无法直接从我的 child 那里调用“CloseModal”——Elm 不允许我调用其他人的消息。我有什么选择?
<小时/>我以为我可以调用“Modal.Update.update Modal.Messages.CloseModal”,但在我的组件内部我只有状态 block 。所以这不是一个选择。
然后我找到了一种将消息从父级传递给子级的方法,但这并不能帮助我以其他方式传递消息。或者 sibling 。
最佳答案
简而言之,您无法将消息直接从子级传递给父级或兄弟级。
Elm 架构实现了单向消息传递,换句话说,在子组件接收消息之前,您的父组件始终知道子组件的消息。
我做了一个简单的example of parent-child communication ,它太大了,无法将其嵌入到答案中,因此我只会在此处记下要点。
子组件定义了 set of Messages :
type Msg
= Update Model
| Focus
| Blur
在它的update
函数中,我们ignore消息,用于父组件。
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Update value ->
( value, Cmd.none )
-- Ignore the rest of the messages.
_ ->
( model, Cmd.none )
在父级的 update
函数中,我们可以对所需消息进行模式匹配并对它们使用react。
其余消息将通过 default branch 。
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NameMsg childMsg ->
case childMsg of
{- We have intercepted a message from child component.
This part of the update function might be moved
to a separate function for better readability.
-}
Input.Focus ->
update (HelperMsg Helper.Show) model
Input.Blur ->
update (HelperMsg Helper.Hide) model
-- The default message passing routine.
_ ->
let
( nameModel, nameCmd ) =
Input.update childMsg model.name
in
( { model | name = nameModel }
, Cmd.map NameMsg nameCmd
)
上面的例子总结了 child - parent 和 sibling 的沟通。您可以根据需要对发送给任何组件的任何消息递归地运行更新函数。
更新
功能发送消息Cmd.Extra公开一个发送消息的函数。
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model ->
(model, message SomeMessage)
PS:翻译模式示例位于我的待办事项中,如果您希望我用它更新答案,请发表评论。
关于elm - 如何从 Elm 组件中调用父消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38827491/
基本上我想在悬停时使文本带有删除线。这并不容易使用 el [ mouseOver [Font.strike] ] (text "some text") 就像这样 el [ mouseOver [Bac
基本上我想在悬停时使文本带有删除线。这并不容易使用 el [ mouseOver [Font.strike] ] (text "some text") 就像这样 el [ mouseOver [Bac
以下模式匹配至少有两个元素的列表有什么问题pos1和 pos2 ? type Pos = (Float, Float) type Tail = [Pos] tail_cut : Float -> Ta
我想更新 Elm 类型的记录 type CubeData = {currDirection : Vec3, translation : Vec3, transform : Mat4} 我可以使用以下代
假设有两种数据类型: type alias Player = { name : String , team : Team } type alias Team = { name : St
Python 有它们,我发现它们非常有用: def awesome_fn(x, y): """" Calculates some awesome function of x and y
我正在尝试在 Elm 中设计一个功能,该功能解析来自 Json 的数据,然后将其呈现在一个可排序的表中。 当然,我使用解码器将 Json 数据存储在记录列表中;然后在 View 中,我将记录列表转换为
假设我正在尝试遵循 Elm 架构并将我的工作流程拆分为 User s 和 Invoice s 使用 StartApp . 用户有发票,但他们必须登录才能访问它们。 该模型可能看起来像这样: type
我喜欢 React/Redux 的智能和哑组件概念,其中哑组件不处理自己的状态(Dump 组件对外界一无所知,它所做的只是根据其输入触发事件并显示值) .这是微不足道的,因为所有状态都在一个地方处理(
如何在 elm-repl 中找出 Elm 表达式或子表达式的类型? Haskell 的 :type或 :t相当于 Elm REPL 吗? 最佳答案 Elm REPL 会自动打印您输入的任何类型。例如:
在 Elm 中是否可以通过一个事件处理程序触发多个输入句柄?例如,假设我有以下内容: button [ type' "button", on "click" getAnything go.ha
我正在尝试构建一个通用映射函数,该函数将从联合类型中提取可能的内部类型,对内部值应用转换并将其映射回外部类型。 问题是,我需要一种方法来区分外部类型的特定值是否具有内部类型。 如果下面的代码实际编译了
每隔一段时间,您就会遇到如下编译器错误: It looks like the keyword `port` is being used as a variable. 这很烦人。这些关键词有完整的官方列
我刚刚开始考虑使用 Elm 构建一个简单的 Web 应用程序。我的想法是需要在浏览器中保留一些用户数据。 有没有办法直接用 Elm 处理数据持久化?例如在浏览器 session 甚至本地存储中?或者我
我有这个功能 result = add 1 2 |> \a -> a % 2 == 0) 我收到了这个错误 Elm does not use (%) as the remainder operat
编写一个函数将一个列表分成两个列表。第一部分的长度由调用者指定。 我是 Elm 的新手,所以我不确定我的推理是否正确。我认为我需要转换数组中的输入列表,以便我能够按提供的输入数字对其进行切片。我也在语
我正在尝试创建一个简单的Elm项目,仅插入“hello world!”串成div。 这是我的代码: index.html: ELM Course
我正在编写elm程序,该程序应将其输出格式化为HTML列表。我要的功能 inputs = ["first", "second", "third"] 并输出本质上是某种Elm Element,
在 Elm 中,我无法弄清楚什么时候 type 是合适的关键字,什么时候 type 别名 才是合适的。文档似乎没有对此进行解释,我也无法在发行说明中找到这一解释。这是否记录在某处? 最佳答案 我的想法
我刚刚开始学习 Elm,并且在理解为什么将自定义类型传递到期望的方法时遇到类型不匹配的问题......好吧,我称之为部分类型注释. 这是我正在使用的代码: import Graphics.Elemen
我是一名优秀的程序员,十分优秀!