简体   繁体   English

聚合物和外部node.js库

[英]Polymer and external node.js Libraries

I am working with Polymer 1.0 and now it's time to add an external .JS library. 我正在使用Polymer 1.0,现在是时候添加一个外部.JS库了。
The library I want to add is called SuperAgent and it is a Node.js module capable to provide powerful APIs for XMLHttpRequests. 我要添加的库称为SuperAgent,它是一个Node.js模块,能够为XMLHttpRequests提供强大的API。 It is also suggested by Polymer's team. Polymer的团队也建议这样做。

Step 01 - Add bower module 步骤01-添加Bower模块

$ bower install --save superagent

Step 02 - Add <script> reference 步骤02-添加<script>参考

<!-- SuperAgent -->
<script 
   type="text/javascript" 
   src="/bower_components/superagent/lib/client.js"></script>

Now I get an error in Chrome saying: 现在,我在Chrome中收到一条错误消息:

Uncaught ReferenceError: require is not defined

If I inspect the client.js file, it has these statements at the beginning, which are the cause of my error: 如果我检查client.js文件,它的开头有这些语句,这是导致我出错的原因:

/**
 * Module dependencies.
 */

var Emitter = require('emitter');
var reduce = require('reduce');

Now, my project is done using exclusively bower, what do I need to do in order to make "require" understandable by my Polymer app? 现在,我的项目是使用专门的凉亭做,我需要什么才能做,使“规定”由我聚合物的应用程序可以理解的? Is there a bower component + script that do that? 是否有一个Bower组件+脚本可以做到这一点?

I have used SuperAgent before and I am familiar with Polymer (but haven't used it much) 我以前使用过SuperAgent,并且对Polymer熟悉(但是使用的并不多)

SuperAgent is a server side nodeJS npm package used to stand up a server for running a batch of unit tests and it is then shut down. SuperAgent是服务器端的nodeJS npm软件包,用于安装服务器以运行一批单元测试,然后将其关闭。

Polymer is a client side tool used to make the full power of the shadow DOM available now. Polymer是一种客户端工具,用于使影子DOM的全部功能现在可用。

How the 2 work together I have no idea but the require statements used by SuperAgent are nodeJS require statements made to require other modules. 2如何协同工作我不知道,但SuperAgent使用的require语句是nodeJS require语句,需要其他模块。 The reason your browser doesn't understand require is because browsers don't yet understand modularity (although they will do soon with ES6 modules on the way in). 您的浏览器无法理解需求的原因是因为浏览器尚未理解模块化(尽管即将使用ES6模块也可以)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM