我有一个项目 world_app 我已包含在 中hello_app 作为依赖项(如果相关,我已将其作为本地依赖项包含在内)
defp deps do
[
{:world_app, path: "../world_app"}
]
end
world_app 有一个 config.exs 有这个配置
config :world_app, some_config: "config_string"
当我尝试获取 中定义的配置变量时出现问题world_app 在 hello_app (我在 hello_app 中运行了 iex -S mix)
iex(1)> Application.get_all_env(:world_app)
[included_applications: []]
iex(2)> Application.get_env(:world_app, :some_config)
nil
但是,当我在 中做同样的事情时world_app 我可以看到变量
iex(1)> Application.get_all_env(:world_app)
[some_config: "config_string", included_applications: []]
iex(2)> Application.get_env(:world_app, :some_config)
"config_string"
我一直认为我可以从父应用程序访问依赖项的配置;我在这里错过了一些重要的东西吗?
我正在使用 Elixir 1.5.3 和 erlang 20
最佳答案
依赖项的配置不会自动导入。在 umbrella projects ,所有 child 都可以看到彼此的配置,因为根配置包含以下神奇的行:
import_config "../apps/*/config/config.exs"
它导入其所有子项的所有配置文件,相反,其所有子项都指向
mix.exs
中的根配置文件。 :defmodule ChildProject.MixProject do
use Mix.Project
def project do
[
(...)
config_path: "../../config/config.exs",
(...)
]
end
(...)
end
这在 chapter of the Mix & OTP Getting Started Guide 中有所解释。 .
您可以使用相同的技巧通过将此行添加到
hello_app/config/config.exs
来显式导入依赖项的配置。 :import_config "../../world_app/config/config.exs"
关于elixir - 无法访问主 elixir 项目中的依赖项配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50531188/