如何将第三方JavaScript库添加到Meteor应用程序? [英] How can I add third-party JavaScript libraries to a Meteor application?

查看:101
本文介绍了如何将第三方JavaScript库添加到Meteor应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Meteor应用程序中添加一个JavaScript前端插件,如 jquery.center.js

I want to add a JavaScript front-end plugin, like jquery.center.js, to a Meteor app.

如果我将它放在我的 app / 目录中并刷新页面,我会收到此错误:

If I put it in my app/ directory and refresh the page I get this error:


你的应用程序崩溃了。这是最新的日志。

Your app is crashing. Here's the latest log.

node.js:201

throw e; // process.nextTick错误,或第一次打勾时出现'error'事件

^

ReferenceError:未定义jQuery
app / jquery.center中的
。 js:43:1

at /Users/crapthings/Desktop/app/.meteor/local/build/server/server.js:111:21

at Array.forEach(本地)
函数
。 (/Users/crapthings/Desktop/app/.meteor/local/build/server/underscore.js:76:11)

atUsers/crapthings/Desktop/app/.meteor/local/build /server/server.js:97:7

退出代码:1

您的应用程序崩溃了。正在等待文件更改。

node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
ReferenceError: jQuery is not defined
at app/jquery.center.js:43:1
at /Users/crapthings/Desktop/app/.meteor/local/build/server/server.js:111:21
at Array.forEach (native)
at Function. (/Users/crapthings/Desktop/app/.meteor/local/build/server/underscore.js:76:11)
at /Users/crapthings/Desktop/app/.meteor/local/build/server/server.js:97:7
Exited with code: 1
Your application is crashing. Waiting for file change.


推荐答案

您正在将jquery插件javascript文件直接放在app文件夹中,这样就可以为客户端和服务器加载javascript文件。

You are putting jquery plugin javascript file in app folder directly,so that javascript file will be be loaded for client as well as server.

根据Meteor文档:

客户端从以下项目加载javascript:project / public和project / client

服务器从以下项目加载javascript:project / public和project / server folders。

As per Meteor documentation:
Client loads javascript from: project/public and project/client
Server loads javascript from: project/public and project/server folders.

从v1.0开始,Meteor在客户端内部使用jQuery,所以你可以直接使用您的库而不添加jQuery。但是,建议您明确地向您的Meteor项目添加jQuery:

As of v1.0, Meteor is using jQuery internally in the client, so you can use your library directly without adding jQuery. However, it's recommended that you add jQuery to your Meteor project explicitly:


meteor add jquery

meteor add jquery

Meteor文档详细解释了 JavaScript文件的加载方式以及静态资产应该去(CSS,图像)。

The Meteor docs explain in depth how JavaScript files are loaded and where static assets should go (CSS, images).

参见 如何为Meteor重新打包现有的图书馆

See also how to repackage an existing library for Meteor.

这篇关于如何将第三方JavaScript库添加到Meteor应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆