【发布时间】:2013-06-03 23:50:02
【问题描述】:
我正在尝试制作一个多页 Meteor 应用程序(我正在使用 https://github.com/tmeasday/meteor-router)。如何根据页面内容有条件地加载 javascript?
这就是我正在做的:主页main.html 在<body> 标签中有一个{{renderPage}},这是 Meteor-router 使用的。假设有一个名为home 的页面/模板,它是其中一个页面,它有一些脚本。如果不是 Meteor,其中一些脚本位于 <header> 中,例如 jquery,而其他脚本则位于 <body> 的末尾。我在 home 模板的末尾放了一个名为 homeCustomScripts 的部分,它有一个相应的 Template.homeCustomScripts 函数,就像这样(我省略了很多脚本,这只是一个例子):
Template.homeCustomScripts.created = function() {
scriptInsert = function () {
$('head').append('<script type="text/javascript" src="js-plugin/respond/respond.min.js">`
<script type="text/javascript" src="js-plugin/easing/jquery.easing.1.3.js"></script>`
<script type="text/javascript" src="js/custom.js"></script>');
}
}
在main.html 内我有另一个名为customScript 的部分,我在Template.customScript.rendered 内调用scriptInsert()。
它应该可以工作,除了 Chrome 控制台上有一些非常混乱的 Javascript 错误,我不知道这意味着什么:
我猜是因为 Meteor 还在 <head> 中加载了 JQuery,而且我听说 Meteor 最后加载了那里的所有内容,所以可能在该 JQuery 实例之前加载了 JQuery 插件缓动。
但我不知道。
在 Meteor 的多页面应用程序中为不同页面加载不同脚本的最佳方法是什么?
【问题讨论】:
标签: meteor