如何通过凤凰后端从榆树的静态资产中获取图像?

时间:2019-11-03 00:37:57

标签: elixir phoenix-framework

榆树前的图像需要存储在静态图像中的图像:

background : Model -> Html Msg
background model =
    img
        [ src "assets/static/images/main_bg.jpg)"
        ]
        []

我尝试使用priv/static/images/main_bg.jpg,但收到相同的错误:

 ** (Phoenix.Router.NoRouteError) no route found for GET /assets/static/images/main_bg.jpg (PlatformWeb.Router)
    (platform) lib/phoenix/router.ex:324: PlatformWeb.Router.call/2
    (platform) lib/platform_web/endpoint.ex:1: PlatformWeb.Endpoint.plug_builder_call/2
    (platform) lib/plug/debugger.ex:122: PlatformWeb.Endpoint."call (overridable 3)"/2
    (platform) lib/platform_web/endpoint.ex:1: PlatformWeb.Endpoint.call/2
    (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:42: Phoenix.Endpoint.Cowboy2Handler.init/4

1 个答案:

答案 0 :(得分:1)

您正在向端点GET发出/assets/static/images/main_bg.jpg请求,但没有为其定义路由。

如果要提供images文件夹(即priv/static/images/main_bg.jpg)的内容,可以在Phoenix Endpoint(lib/my_app_web/endpoint.ex)中使用Plug.Static

如果您想指定将要投放的内容,请确保将images添加到:only选项:

defmodule MyApp.Endpoint do
  use Phoenix.Endpoint, otp_app: :my_app

  # Serve at "/" the static files from "priv/static" directory.
  #
  # You should set gzip to true if you are running phx.digest
  # when deploying your static files in production.
  plug Plug.Static,
    at: "/",
    from: :my_app,
    gzip: true,
    only: ~w(images asset-manifest.json manifest.json)

最后,别忘了将图像src更改为"images/main_bg.jpg"