问题描述
正在为 render_many
实现 live_component
的模拟。
应该是这样工作的:
<%= exhibit(@collection,SomeComponent,socket: @socket,as: :some) %>
require Phoenix.LiveView.Helpers
alias Phoenix.LiveView.Helpers,as: View
def exhibit(collection,component,params \\ [],socket: socket,as: item_name) when is_list(collection) do
Enum.map(collection,fn item ->
View.live_component socket,some: item
end)
end
好吧,这似乎是不可能的:
组件必须始终作为 LiveView 模板的一部分直接返回。
例如,这是不允许的:
<%= content_tag :div do %>
<%= live_component @socket,SomeComponent %>
<% end %>
那是因为组件在 content_tag
内。然而,这
作品:
<div>
<%= live_component @socket,SomeComponent %>
</div>
在 Elixir 的特殊形式中也允许使用组件,例如
if
、for
、case
和朋友。
<%= for item <- items do %>
<%= live_component @socket,id: item %>
<% end %>
但是,使用其他模块函数(例如 Enum
)将不起作用:
<%= Enum.map(items,fn item -> %>
<%= live_component @socket,id: item %>
<% end %>
尝试了一些其他方法使其工作,但似乎没有希望。
知道如何为 render_many
实现 live_component
还是我不得不忍受模板中那些丑陋的 for
循环?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)