简体   繁体   English

如何在Electron中使用引导滑块?

[英]How do I use bootstrap slider in Electron?

I am getting the following error when I use bootstrap slider ( https://github.com/seiyria/bootstrap-slider ) in my Electron( http://electron.atom.io/docs/latest/tutorial/quick-start/ ) app : 当我在Electron( http://electron.atom.io/docs/latest/tutorial/quick-start/ )中使用引导程序滑块( https://github.com/seiyria/bootstrap-slider )时,出现以下错误)应用:

"Uncaught TypeError: $(...).Slider is not a function" “未捕获的TypeError:$(...)。滑块不是函数”

Earlier I was also struggling with using Jquery but solved it using : https://github.com/atom/electron/issues/254 : 早些时候我也在努力使用Jquery,但使用以下方法解决了它: https : //github.com/atom/electron/issues/254

window.$ = window.jQuery = require('/path/to/jquery'); window。$ = window.jQuery = require('/ path / to / jquery'); instead of regular : 而不是常规的:

The reason quoted was Query contains something along this lines: 引用的原因是查询包含以下内容:

if ( typeof module === "object" && typeof module.exports === "object" ) {
  // set jQuery in `module`
} else {
  // set jQuery in `window`
}

I don't understand what is the right way to use it for bootstrap the slider. 我不知道使用它引导滑块的正确方法是什么。

I could see that bootstrap-slider.js has a component dealing with "module" which might be causing the anomaly just like in jquery. 我可以看到bootstrap-slider.js包含一个处理“模块”的组件,这可能会导致异常,就像在jquery中一样。

(function(root, factory) {
    if(typeof define === "function" && define.amd) {
            define(["jquery"], factory);
    } else if(typeof module === "object" && module.exports) {
            var jQuery; 
            try {   
                    jQuery = require("jquery");
            } catch (err) { 
                    jQuery = null; 
            }       
            module.exports = factory(jQuery);
    } else {
            root.Slider = factory(root.jQuery);
    }       

Please tell me how to deal with this. 请告诉我如何处理。

You have 2 options: 您有2个选择:

  1. Include jQuery and Bootstrap slider regularly in the index.html page with a script after each to make them global like so: 定期在index.html页面中包括jQuery和Bootstrap滑块,并在每个脚本之后分别添加一个脚本,以使其具有全局性,如下所示:

 <script src="bower_components/jquery/dist/jquery.js"></script> <script type="application/javascript"> if (typeof module === 'object' && typeof module.exports !== 'undefined') { window.$ = window.jQuery = module.exports; } </script> <script src="path/to/bootstrap-slider.js"></script> <script type="application/javascript"> if (typeof module === 'object' && typeof module.exports !== 'undefined') { window.Slider = module.exports; } </script> 

  1. Use require to include jQuery and Slider whenever you need them require使用require包括jQuery和Slider

 var $ = require('jquery'); var Slider = require('bootstrap-slider'); 

Found that solution below yesterday. 昨天在下面找到了该解决方案。 Worked for me both with jQuery and Bootstrap Slider. 为我使用jQuery和Bootstrap Slider。 Credits on the bottom. 在底部的学分。

A better an more generic solution IMO: 更好,更通用的解决方案IMO:

<!-- Insert this line above script imports  -->
<script>if (typeof module === 'object') {window.module = module; module = 
undefined;}</script>

<!-- normal script imports etc  -->
<script src="scripts/jquery.min.js"></script>    
<script src="scripts/vendor.js"></script>    

<!-- Insert this line after script imports -->
<script>if (window.module) module = window.module;</script>

Benefits 优点

  • Works for both browser and electron with the same code 使用相同的代码适用于浏览器和电子产品
  • Fixes issues for ALL 3rd-party libraries (not just jQuery) without having to specify each one 修复了所有第三方库(不仅仅是jQuery)的问题,而无需指定每个库
  • Script Build / Pack Friendly (ie Grunt / Gulp all scripts into vendor.js) 脚本构建/打包友好(例如,将所有脚本Grunt / Gulp到vendor.js中)
  • Does NOT require node-integration to be false 不需要节点集成为假

source here 来源在这里

I was able to fix this by placing my path to jquery in the bootstrap-slider.js 我可以通过将我的 jquery路径放在bootstrap-slider.js中来解决此问题

Line 41: jQuery = require("my/path/to/jquery-2.1.4.min.js");

This is probably the wrong solution, but hopefully a bad idea can lead to a good one :) 这可能是错误的解决方案,但希望一个坏主意可以带来一个好主意:)

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

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