简体   繁体   English

如何从 Phoenix LiveView heex 模板调用 Elixir function 并传递来自套接字分配的数据 arguments?

[英]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 的他的mountdefp代码添加了套接字分配。 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.

相关问题 elixir phoenix liveview - 通过套接字传递用户 ID - elixir phoenix liveview - passing user id through socket Elixir Phoenix:我可以从模板调用上下文函数吗,我应该吗? - Elixir Phoenix: Can I call a context function from a template, and should I Elixir / Phoenix LiveView:如何向Rollbar报告异常? - Elixir / Phoenix LiveView: How can I report exceptions to Rollbar? 如何将 LiveView 添加到现有的 Elixir/Phoenix 应用程序? - How can I add LiveView to an existing Elixir/Phoenix app? 如何在我的 Elixir Phoenix LiveView 应用程序 header 中混合实时和“死”内容? - How do I mix live and "dead" content in my Elixir Phoenix LiveView app header? 从“凤凰”功能导入{Socket,Presence}不起作用。 凤凰/药剂 - import {Socket, Presence} from “phoenix” function not working. Phoenix/Elixir 如何从 LiveView Phoenix 更新 LiveComponent - how to update LiveComponent from LiveView Phoenix 如何使用 Phoenix Guides 中的示例在生产模式下运行 Elixir Phoenix Docker? - How do I run Elixir Phoenix Docker in production mode using example from Phoenix Guides? 在挂载期间使用 LiveView 将值从客户端传递到 Phoenix 服务器 - Passing a value from client to Phoenix server using LiveView during mount 如何从破坏 Elixir Phoenix 的地方恢复 Elixir 任务 - how to resume an elixir task from where it broke Elixir Phoenix
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM