[英]How do I call an Elixir function from a Phoenix LiveView heex template passing arguments with data from socket assigns?
I'm trying to make use of the code in this blog post: [Phoenix LiveView] formatting date/time with local time zone .我正在尝试使用这篇博文中的代码: [Phoenix LiveView] formatting date/time with local time zone 。
I confirmed (with IO.inspect
) that his mount
and defp
code I added to a LiveView adds the socket assigns.我确认(使用
IO.inspect
)我添加到 LiveView 的他的mount
和defp
代码添加了套接字分配。 But he doesn't give an example of how to call the format_time
function from a heex template.但他没有给出如何从 heex 模板调用
format_time
function 的示例。 Perhaps it should be obvious, but it's not obvious to me.也许它应该是显而易见的,但对我来说并不明显。 I tried this:
我试过这个:
<%= 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
%>
But I get this error:但我得到这个错误:
[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
...
I guess I can't use the socket assigns while rendering ?我想我不能在渲染时使用套接字分配?
The socket assigns are available directly inside the template as LiveEEx assigns, such as @foo
and @bar
.套接字分配可直接在模板内部使用,如 LiveEEx 分配,例如
@foo
和@bar
。 Any assign access should be done using the assigns in the template where proper change tracking takes place.任何分配访问都应该使用模板中的分配来完成,在模板中进行适当的更改跟踪。
Here are the official docs: Phoenix.LiveView.Socket.AssignsNotInSocketView这是官方文档: Phoenix.LiveView.Socket.AssignsNotInSocketView
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.