【发布时间】:2015-07-11 14:53:59
【问题描述】:
在单个 Phoenix 模板中要求定义的 JavaScript 模块的标准方法是什么?
除了这个模板之外,我不希望任何地方都需要该模块。
这是我正在使用的文件的 sn-p。
web/static/js/trend_chart.js
let TrendChart = {
//... some JS module code here
}
web/templates/layout/app.html.eex
这具有标准的应用加载/要求。
...
<script src="<%= static_path(@conn, "/js/app.js") %>"></script>
<script>require("web/static/js/app")</script>
...
web/templates/page/index.html.eex
<!-- what do i put in this template to require / load the TrendChart module code? -->
<!-- i don't want it required globally, so i don't want to put it in the app.html.eex file -->
更新 #1
我真的在寻找一种在主布局中有两个 @inner 块的方法。一种用于内容,另一种用于在内容之后加载的附加 JavaScript 项。
类似于 ASP.NET MVC 中的sections。 (我知道,我知道!)
所以app.html.eex 最终会是这样的:
...
@inner
...
<script src="<%= static_path(@conn, "/js/app.js") %>"></script>
<script>require("web/static/js/app")</script>
*something here to load page/template specific javascript*
【问题讨论】:
-
所以,我将
<script src="<%= static_path(@conn, "/js/app.js") %>"></script>放在@inner之前,这样我就可以在单个页面模板中使用加载的JS 库了。我希望有另一种方法可以在特定页面的@inner之后附加 JS。 -
@toraritte 此链接已失效。可能下次在此处包含相关代码比单独使用外部链接更好。
-
This article 是我的答案。
-
谢谢。无需删除您的原始评论 - 它可能会混淆其他人。
标签: javascript html phoenix-framework