[英]How do I call an Elixir function from a Phoenix LiveView heex template passing arguments with data from socket assigns?
我正在尝试使用这篇博文中的代码: [Phoenix LiveView] formatting date/time with local time zone 。
我确认(使用IO.inspect
)我添加到 LiveView 的他的mount
和defp
代码添加了套接字分配。 但他没有给出如何从 heex 模板调用format_time
function 的示例。 也许它应该是显而易见的,但对我来说并不明显。 我试过这个:
<%= if connected?(@socket) do
Cldr.format_time(@auction.started_or_paused_at,
locale: @socket.assigns.locale,
timezone: @socket.assigns.timezone)
else
Cldr.format_time(@auction.started_or_paused_at)
end
%>
但我得到这个错误:
[error] GenServer #PID<0.655.0> terminating
** (KeyError) key :locale not found in: #Phoenix.LiveView.Socket.AssignsNotInSocket<>
(ssauction_live 0.1.0) lib/ssauction_live_web/live/auction_live/show.html.heex:32: anonymous fn/2 in SSAuctionWeb.AuctionLive.Show.render/1
...
我想我不能在渲染时使用套接字分配?
套接字分配可直接在模板内部使用,如 LiveEEx 分配,例如@foo
和@bar
。 任何分配访问都应该使用模板中的分配来完成,在模板中进行适当的更改跟踪。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.