本文介绍了在Derby.js中包含JS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习,我遇到了很多麻烦。我知道我可以通过npm包含诸如jQuery之类的软件包并将其添加到node_modules文件夹中,但这不是我想要做的。我希望能够像普通HTML一样包含这些文件。

I am trying to learn Derby.js and I am having a lot of trouble. I know I can include packages such as jQuery through npm and add it to the node_modules folder, but this isn't quite what I want to do. I want to be able to include these files like I do in normal HTML.

所以我想做一些像< Head:> < script src =js / jquery.js>< / script> 。这不起作用,因为它找不到js目录。我希望这与node.js运行应用程序的方式有关,并且应用程序本身不会保存js目录。

So I want to do something like <Head:> <script src="js/jquery.js"></script>. This does not work though because it cannot find the js directory. I expect this has something to do with the way node.js runs an app and that the app itself will not hold the js directory.

任何帮助都将不胜感激!

Any help would be appreciated!

推荐答案

德比提供脚本:标签:

<Scripts:>
    <script type="text/javascript" src="/components/jquery/jquery.js"></script>

组件目录是因为。将组件目录放入 public 目录中。根据,静态路由搜索给定目录下的 (这在德比的示例应用程序中是公开的)。配置bower将文件放在 public / components 下()。

The components directory is because of the usage of bower. Put the components directory into the public directory. According to the express FAQ, the static routes search below the given directory (which is public in derby's example application). Configure bower to put the files under public/components (Choose bower install directory).

公共目录配置在 lib / server / index.js .use(gzippo.staticGzip(publicPath,{maxAge:ONE_YEAR})),其中 publicPath 配置如上到 path.join(root,'public')

The public directory is configured at lib/server/index.js: .use(gzippo.staticGzip(publicPath, {maxAge: ONE_YEAR})), where publicPath is configured above to path.join(root, 'public').

请注意内联脚本背后的想法是在它加载任何外部脚本之前立即运行。这应该仅在极少数情况下使用,在脚本应该在浏览器中显示页面之前运行,例如调整窗口大小或在浏览器中自动对齐元素不支持自动对焦属性。 。

Be aware that the "idea behind the inline script is that it runs immediately, before any of the external scripts are loaded. This should only be used in rare cases where the script should run before the page is displayed in the browser, such as sizing something to the window or autofuocusing an element in browsers that don't support the "autofocus" attribute." Nate Smith in the derby google group.

内联脚本应放在 inline.js 中,与应用程序的 index.js位于同一目录中

Inline scripts should be placed in inline.js, located in the same directory as the application's index.js.

如果您需要jQuery在加载的页面上执行某些操作,请执行以下代码剪切工作(Firefox,Chrome)在 inline.js

If you require jQuery to do something on the loaded page, following code snipped worked at my side (Firefox, Chrome) in inline.js:

window.onload = function() {
    alert($(this));
}

这篇关于在Derby.js中包含JS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:33