Sails.js - 如何将js文件注入特定路由? [英] Sails.js - How to inject a js file to a specific route?
问题描述
例如,我有一个页面 / locations / map
我需要包含 Google地图库,并且仅包含 .js 文件(例如location.js)。
For example, I have a page /locations/map
which I need to include Google Map library, and include a .js file (e.g. location.js) specifically for this page only.
我想将这两个文件注入<! - SCRIPTS END - >
此行
I want to inject these 2 files to after <!--SCRIPTS END-->
this line
是否可以这样做?
注意:我使用的是Sails.js v0。 10
推荐答案
Sails在其中使用 ejs-locals
查看渲染,因此您可以使用块完成所需的操作。
Sails uses ejs-locals
in its view rendering, so you can accomplish what you want with blocks.
在 layout.ejs
文件中,在<! - SCRIPTS END下面 - >
,添加(例如):
In your layout.ejs
file, underneath the <!--SCRIPTS END-->
, add (for example):
<%- blocks.localScripts %>
然后在视图中,您将在 / locations / map
,使用脚本标记调用块,例如:
Then in the view you're serving at /locations/map
, call the block with your script tag, for example:
<% block('localScripts', '<script src="https://maps.googleapis.com/maps/api/js"></script>') %>
作为替代方案,你可以把<! - SCRIPTS- - >
和<! - SCRIPTS END - >
< head>
您的布局,然后将特定于视图的脚本直接添加到视图中而不是使用块。如果您不介意在显示页面内容之前等待加载这些链接脚本,这是一个不错的选项。
As an alternative, you could put the <!--SCRIPTS-->
and <!--SCRIPTS END-->
tags in the <head>
of your layout, and then add your view-specific scripts directly into your view rather than using blocks. This is a fine option if you don't mind waiting for those linked scripts to load before your page content is displayed.
这篇关于Sails.js - 如何将js文件注入特定路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!