【问题标题】:JS.__struct__/1 is undefined, cannot expand struct JSJS.__struct__/1 未定义,无法扩展 struct JS
【发布时间】:2021-12-09 05:00:18
【问题描述】:

我尝试浏览 LiveView 0.17.1 文档 (https://hexdocs.pm/phoenix_live_view/0.17.1/Phoenix.LiveView.JS.html) 的 JS 示例

所以我使用mix phx.new demo3 --no-ecto 创建了一个新的Phoenix 1.6.2 应用程序并将mix.exs deps 部分更改为:

  defp deps do
    [
      {:phoenix, "~> 1.6.2"},
      {:phoenix_html, "~> 3.0"},
      {:phoenix_live_reload, "~> 1.2", only: :dev},
      {:phoenix_live_view, "~> 0.17.1"},
      {:floki, ">= 0.30.0", only: :test},
      {:phoenix_live_dashboard, "~> 0.5"},
      {:esbuild, "~> 0.2", runtime: Mix.env() == :dev},
      {:swoosh, "~> 1.3"},
      {:telemetry_metrics, "~> 0.6"},
      {:telemetry_poller, "~> 1.0"},
      {:gettext, "~> 0.18"},
      {:jason, "~> 1.2"},
      {:plug_cowboy, "~> 2.5"}
    ]
  end

之后,我创建了一个 LiveView 页面并将此示例代码添加到其中:

  def hide_modal(js \\ %JS{}) do
    js
    |> JS.hide(transition: "fade-out", to: "#modal")
    |> JS.hide(transition: "fade-out-scale", to: "#modal-content")
  end

启动服务器失败:

$ mix phx.server
Compiling 1 file (.ex)

== Compilation error in file lib/demo3_web/live/stock_watch_live.ex ==
** (CompileError) lib/demo3_web/live/stock_watch_live.ex:66: JS.__struct__/1 is undefined, cannot expand struct JS. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code
    lib/demo3_web/live/stock_watch_live.ex:66: (module)

我必须添加/更改什么来解决这个问题?

【问题讨论】:

  • 您是否可能忘记给其他模块起别名,例如alias Javascript, as: JS ?
  • 我认为你定义hide_modal的文件顶部缺少的是alias Phoenix.LiveView.JS
  • 此链接github.com/phoenixframework/phoenix/issues/4642 可能会有所帮助。

标签: elixir phoenix-framework phoenix-live-view


【解决方案1】:

在文件顶部添加alias Phoenix.LiveView.JS 可以解决问题。感谢@scott-thompson!

文档刚刚更新。 https://github.com/phoenixframework/phoenix_live_view/commit/a0b39649ce6f02561033fd42c7205644fe3a706b#diff-a095f1aa9c79bb58142c5628142a5ddc52bb344547400409a8c6a3237896cedc

【讨论】:

    猜你喜欢
    • 2018-07-19
    • 2020-08-27
    • 1970-01-01
    • 2020-02-11
    • 2016-03-25
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多