ijt*_*ijt 7 elixir phoenix-framework phoenix-live-view
我正在编写一个 ClockComponent 来了解 Phoenix LiveComponents。我几乎拥有它,但它正在向其父级发送 :tick 消息。我怎样才能让它向自己发送该消息?我想用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
Run Code Online (Sandbox Code Playgroud)
我认为你不能基于此: https: //hexdocs.pm/phoenix_live_view/Phoenix.LiveComponent.html#module-managing-state
组件在 LiveView 进程内运行,但可能有自己的状态和事件处理。
我相信“子组件”和父组件共享相同的过程。