Javascript js-如何将js文件注入特定路由?
例如,我有一个页面Javascript js-如何将js文件注入特定路由?,javascript,node.js,sails.js,inject,Javascript,Node.js,Sails.js,Inject,例如,我有一个页面/locations/map,我需要将其包含在库中,并包含一个专门用于此页面的.js文件(例如location.js) 我想在这一行之后插入这两个文件 有可能这样做吗 注意:我使用的是Sails.js v0.10Sails在其视图渲染中使用了ejs locals,因此您可以使用块完成所需的操作 在layout.ejs文件中,在下面添加(例如): 或者,您可以将和标记放在布局的中,然后直接将视图特定脚本添加到视图中,而不是使用块。如果您不介意在显示页面内容之前等待加载这些链接脚本
/locations/map
,我需要将其包含在库中,并包含一个专门用于此页面的.js文件(例如location.js)
我想在
这一行之后插入这两个文件
有可能这样做吗
注意:我使用的是Sails.js v0.10Sails在其视图渲染中使用了
ejs locals
,因此您可以使用块完成所需的操作
在layout.ejs
文件中,在
下面添加(例如):
或者,您可以将
和
标记放在布局的
中,然后直接将视图特定脚本添加到视图中,而不是使用块。如果您不介意在显示页面内容之前等待加载这些链接脚本,那么这是一个不错的选择。我发现了其他方法
在MapController.js中
// you can add as many as you like
res.locals.scripts = [
'//maps.googleapis.com/maps/api/js',
];
return res.view();
<!--SCRIPTS-->
<!--SCRIPTS END-->
<!-- Loop through all scripts that passed from controller -->
<% if (scripts) { %>
<% for (i = 0; i < scripts.length; i++) { %>
<script src="<%- scripts[i] %>"></script>
<% } %>
<% } %>
在布局中.ejs
// you can add as many as you like
res.locals.scripts = [
'//maps.googleapis.com/maps/api/js',
];
return res.view();
<!--SCRIPTS-->
<!--SCRIPTS END-->
<!-- Loop through all scripts that passed from controller -->
<% if (scripts) { %>
<% for (i = 0; i < scripts.length; i++) { %>
<script src="<%- scripts[i] %>"></script>
<% } %>
<% } %>
的答案是将非全局JS插入特定视图的正确方法。不过,还是有一点注释:来自视图的块调用不应该有破折号。其内容应如下:
两个调用都可以,但使用破折号可以插入两次;加载视图并在布局渲染之前,然后在将视图插入渲染的基本布局时再次加载视图。这不仅会导致不必要地插入/运行两次相同的代码,而且还会导致错误,如果插入的脚本依赖于基本布局中的库(例如jQuery、主干),则会中断JS代码
EJS解释魔法我知道这是一个老问题,因为我发现了另一个选项
文件:
config\routes.js
代码:
我在HomeController中设置StartHome函数,负责管理“/”路由
文件:
api\controllers\HomeController.js
代码:
在这里,我创建了一个带有一些数据的对象,这些数据被传递到EJS模板(到客户端)
文件:
views\layout.ejs
代码:
在my layouts.ejs中,我创建了if语句,该语句根据我当前所在的视图“启用”脚本标记
我就是这样处理的。我希望你能明白
Sails 0.12.4此方法允许灵活地从任何页面本地提供js文件,还可以防止依赖项导致的任何引用错误
在pipeline.js中插入!js/local/*.js在jsFilesToInject的底部,如下所示:
var jsFilesToInject = [
// Load sails.io before everything else
'js/dependencies/sails.io.js',
// Dependencies like jQuery, or Angular are brought in here
'js/dependencies/jquery-3.3.1.min.js',
'js/dependencies/**/*.js',
// All of the rest of your client-side js files
// will be injected here in no particular order.
'js/**/*.js',
//Ignore local injected scripts
'!js/local/*.js'
];
在/assets/js文件夹ie/assets/js/local/中创建一个local文件夹。将任何本地注入的脚本放在这里
在主视图中,ejs ie布局。ejs在脚本块下方插入,如下所示:
<!--SCRIPTS-->
<script src="/js/dependencies/sails.io.js"></script>
<script src="/js/dependencies/jquery-3.3.1.min.js"></script>
<script src="/js/dependencies/bootstrap.min.js"></script>
<script src="/js/dependencies/popper.min.js"></script>
<!--SCRIPTS END-->
<%- blocks.localScripts %>
<% block('localScripts', '<script src="/js/local/homepage.js"></script>') %>
哇~真管用。但是,是否可以在MapController
中插入而不是在视图中?在控制器操作中,您可以设置res.locals.gmapScript=,这意味着无论如何,
这一行必须放在视图中?是的,向视图中添加文本的唯一方法(除了使用链接器,即
标记)是将文本放在模板中。这样做的好处是,您总是打开该视图文件并了解其中的内容。如果您在发送响应之前开始向控制器中的视图添加文本,那么调试就会困难得多!初始解决方案()起作用,但我在块中放置的所有内容都会出现两次—一次在视图中声明,另一次在定义blocks.localScripts的布局中。这种情况会发生吗?(我通过在html注释中的视图中添加块解决了此问题)我认为这可能是Im之后解决此问题的解决方案:。我想问一下,我们有没有办法让脚本更漂亮?@hjbolide我的sailsjs项目已经暂停,如果你能找到其他更好的方法,那就继续吧。我现在正在做更多的ios项目,在这方面帮不了你太多。非常感谢,我的项目正在使用你的解决方案,我喜欢它!感谢您的分享。我花了一些时间来理解逻辑,但这是一个天才:“!js/local/*.js”阻止sails从此位置加载js。加载代码,如果通知,则指向本地脚本。告诉应该加载哪个文件。谢谢你的回答!!!
var jsFilesToInject = [
// Load sails.io before everything else
'js/dependencies/sails.io.js',
// Dependencies like jQuery, or Angular are brought in here
'js/dependencies/jquery-3.3.1.min.js',
'js/dependencies/**/*.js',
// All of the rest of your client-side js files
// will be injected here in no particular order.
'js/**/*.js',
//Ignore local injected scripts
'!js/local/*.js'
];
<!--SCRIPTS-->
<script src="/js/dependencies/sails.io.js"></script>
<script src="/js/dependencies/jquery-3.3.1.min.js"></script>
<script src="/js/dependencies/bootstrap.min.js"></script>
<script src="/js/dependencies/popper.min.js"></script>
<!--SCRIPTS END-->
<%- blocks.localScripts %>
<% block('localScripts', '<script src="/js/local/homepage.js"></script>') %>
$(window).on('load', function(){
initThisPageVariables();
initThisPageBindings();
$(window).on("resize", function(){
initThisPageVariables();
}).resize();
startThisPageApp();
});