如何将第三方 JavaScript 库添加到 Meteor 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11009971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I add third-party JavaScript libraries to a Meteor application?
提问by crapthings
I want to add a JavaScript front-end plugin, like jquery.center.js
, to a Meteor app.
我想向jquery.center.js
Meteor 应用程序添加一个 JavaScript 前端插件,例如。
If I put it in my app/
directory and refresh the page I get this error:
如果我把它放在我的app/
目录中并刷新页面,我会收到这个错误:
Your app is crashing. Here's the latest log.
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.
你的应用程序崩溃了。这是最新的日志。
node.js:201
抛出 e; // process.nextTick 错误,或第一个滴答时的“错误”事件
^
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
在 Array.forEach(本机)
在函数。(/Users/crapthings/Desktop/app/.meteor/local/build/server/underscore.js:76:11)
在 /Users/crapthings/Desktop/app/.meteor/local/build/server/server.js: 97:7
退出,代码:1
您的应用程序崩溃。等待文件更改。
采纳答案by Nachiket
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.
您将 jquery 插件 javascript 文件直接放在 app 文件夹中,以便为客户端和服务器加载 javascript 文件。
As per Meteor documentation:
Client loads javascript from: project/public and project/client
Server loads javascript from: project/public and project/server folders.
根据 Meteor 文档:
客户端从:project/public 和 project/client
服务器加载 javascript:project/public 和 project/server 文件夹。
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:
从 v1.0 开始,Meteor 在客户端内部使用 jQuery,因此您可以直接使用您的库,而无需添加 jQuery。但是,建议您将 jQuery 显式添加到 Meteor 项目中:
meteor add jquery
流星添加jquery
The Meteor docs explain in depth how JavaScript files are loaded and where static assets should go(CSS, images).
Meteor 文档深入解释了 JavaScript 文件是如何加载的以及静态资源应该放在何处(CSS、图像)。
See also how to repackage an existing library for Meteor.
另请参阅如何为 Meteor 重新打包现有库。
回答by Tamara Wijsman
Put it inside the client
folder such that it is only loaded on the client, no need for jQuery on server.
把它放在client
文件夹中,这样它只在客户端加载,不需要在服务器上加载 jQuery。
回答by Gajen Sunthara
One way to do this in MeterorJS 1.3.x
在 MeterorJS 1.3.x 中执行此操作的一种方法
Add the JS files in the public\js\
directory
在public\js\
目录中添加JS文件
Load it up from Meteor.startup method using $.getScript in client/main.js If you want to control script load sequence, control with multiple $.getScript for each js files.
在客户端/main.js 中使用 $.getScript 从 Meteor.startup 方法加载它 如果要控制脚本加载顺序,请为每个 js 文件使用多个 $.getScript 进行控制。
Meteor.startup(function(){
$.getScript('js/fhir-client.js', function(){
// script should be loaded and do something with it.
});
});
回答by Dan Dascalescu
Starting with Meteor 1.3, you can add 3rd party JavaScript libraries to a Meteor project directly via their npm package:
从 Meteor 1.3 开始,您可以通过它们的 npm 包直接将 3rd 方 JavaScript 库添加到 Meteor 项目中:
meteor npm install --save moment
Both server-side and client-side package work without modification because Meteor's ES2015 module system takes care of creating a Node-like environment in the client much as browserify or webpack do.
服务器端和客户端包都无需修改即可工作,因为 Meteor 的 ES2015 模块系统负责在客户端创建类似 Node 的环境,就像 browserify 或 webpack 所做的一样。
If an npm package happens to not function correctly, look for a wrapper on Atmoshpere. Atmosphere is Meteor's official 3rd package repository, but less and less relevant after Meteor v1.3. It will eventuall be phased out.
如果 npm 包碰巧无法正常运行,请在Atmoshpere上查找包装器。Atmosphere 是 Meteor 的官方第三个软件包存储库,但在 Meteor v1.3 之后越来越不相关。它最终将被淘汰。
History
历史
Before Meteor 1.3, you had to repackage 3rd party libraries for Meteor. A tool called Autopublish was developed to automate the process. After the Meteor Development Group stopped offering free hosting at meteor.com, Autopublish was discontinued.
在 Meteor 1.3 之前,您必须为 Meteor 重新打包第 3 方库。开发了一个名为 Autopublish 的工具来自动化该过程。在 Meteor Development Group 停止在meteor.com 上提供免费托管后,Autopublish 停止了。