twitter-bootstrap 如何解决 Bootstrap node.js 应用程序中的错误“无法设置未定义的属性 'emulateTransitionEnd'”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31067196/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 22:52:05  来源:igfitidea点击:

How to solve error "Cannot set property 'emulateTransitionEnd' of undefined" in Bootstrap node.js app?

jquerynode.jstwitter-bootstrap

提问by yojna

I created Node.js application from scratch and I have added in app.js.

我从头开始创建 Node.js 应用程序,并在 app.js 中添加。

global.jQuery = require('jquery'); 

After that it gives me error as below:

之后它给了我如下错误:

/home/yojna/web/node_modules/bootstrap/js/transition.js:36
  $.fn.emulateTransitionEnd = function (duration) {
                        ^
    TypeError: Cannot set property 'emulateTransitionEnd' of undefined
    at /home/yojna/web/node_modules/bootstrap/js/transition.js:36:29
    at Object.<anonymous>(/home/yojna/web/node_modules/bootstrap/js/transition.js:59:2)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> 
    (/home/yojna/web/node_modules/bootstrap/dist/js/npm.js:2:1)
    at Module._compile (module.js:456:26)
    yojna@yojna-Inspiron-7520:~/web$ 

采纳答案by pjehan

NPM is made to include server-side libraries. To include client-side libraries such as bootstrap or jquery, you shoud use Bowerinstead.

NPM 包含服务器端库。要包含诸如 bootstrap 或 jquery 之类的客户端库,您应该改用Bower

I've done this in an expressjs app :

我已经在一个 expressjs 应用程序中做到了这一点:

  1. Create a .bowerrc file and write the following lines :

    {
      "directory" : "public/components"
    }
    
  2. Download bootstrap and jquery with Bower :

    bower install bootstrap
    
  3. Then, define your public folder to be static in your app.js file :

    app.use(express.static(__dirname + '/public'));
    
  4. Finally, add the script tag in your view (I'am using Jade here) :

    script(src='components/jquery/dist/jquery.min.js')
    script(src='components/bootstrap/dist/js/bootstrap.min.js')
    
  1. 创建一个 .bowerrc 文件并写入以下几行:

    {
      "directory" : "public/components"
    }
    
  2. 使用 Bower 下载引导程序和 jquery:

    bower install bootstrap
    
  3. 然后,在 app.js 文件中将公共文件夹定义为静态:

    app.use(express.static(__dirname + '/public'));
    
  4. 最后,在您的视图中添加脚本标签(我在这里使用 Jade):

    script(src='components/jquery/dist/jquery.min.js')
    script(src='components/bootstrap/dist/js/bootstrap.min.js')
    

回答by jcubic

Try declare dollar sign variable:

尝试声明美元符号变量:

global.jQuery = global.$ = require('jquery'); 

回答by Ryan Shillington

I had this problem, and it was because I had included "bootstrap" in my vendor webpack 2 config but was using:

我遇到了这个问题,这是因为我在我的供应商 webpack 2 配置中包含了“引导程序”,但正在使用:

require("bootstrap-loader");

So the browser couldn't properly find the bootstrap-loader package.

所以浏览器无法正确找到 bootstrap-loader 包。

回答by michelem

You shouldn't requireit. Jquery is a client side library while Node.js is a backend server.

你不require应该。Jquery 是一个客户端库,而 Node.js 是一个后端服务器。

If you want to use Jquery with your Node.js app, simply add it to your HTML files with:

如果你想在你的 Node.js 应用程序中使用 Jquery,只需将它添加到你的 HTML 文件中:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>