繁体   English   中英

如何让 Elixir/Phoenix LiveComponent 向其自身而不是其父级发送消息?

[英]How can I make an Elixir/Phoenix LiveComponent send a message to itself, not its parent?

我正在编写一个 ClockComponent 来了解 Phoenix LiveComponents。 我几乎拥有它,但它正在向其父级发送:tick 消息。 我怎样才能让它将该消息发送给自己? 我想使用 self( myself()而不是self()但显然这不是一回事。

defmodule ClockComponent do
  use Phoenix.LiveComponent

  @impl true
  def mount(socket) do
    if connected?(socket), do: :timer.send_interval(1000, self(), :tick)
    {:ok, assign(socket, date: :calendar.local_time())}
  end

  def handle_info(:tick, socket) do
    {:noreply, assign(socket, date: :calendar.local_time())}
  end

  @impl true
  def render(assigns) do
    ~L"""
    The time is <%= @date |> Timex.to_datetime("America/Denver") |> Timex.format!("{RFC1123}") %>
    """
  end
end

我认为您不能基于此: https://hexdocs.pm/phoenix_live_view/Phoenix.LiveComponent.html#module-managing-state

组件在 LiveView 进程内运行,但可能有自己的 state 和事件处理。

我相信“子组件”和父组件都共享相同的过程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM