2016-04-20 4 views
1

У меня есть следующий код:.

var $ = require('jquery'); 
var velocity = require('velocity-animate'); 

module.exports = function() { 

    function togglePanel() { 

     $('.trip-assist-search-panel__container').velocity({ 
      height: '0px' 
     },{ 
      duration: 400 
     }); 

    } 

    return { 
     togglePanel: togglePanel 
    }; 

}(); 

Когда togglePanel() сработал, следующее сообщение об ошибке брошено:

Uncaught TypeError: $(...).velocity is not a function

Какие обычно решается путем обеспечения загрузки JQuery перед библиотекой, которая его требует. Но, я ..

var $ = require('jquery'); // first 
var velocity = require('velocity-animate'); // second 

Итак .. что дает?

+0

Я не понимаю, почему вы назначаете jQuery локальному var, а не глобальному - только глобальная скорость может его использовать. Я также не понимаю, почему вы также назначаете требование скорости локальной переменной. – Seika85

ответ

4

От the documentation:

Module Loader: Browserify
If you're using Velocity with jQuery, you must require jQuery before Velocity, and you must assign jQuery globally on the window object:

window.jQuery = window.$ = require("path/to/jquery-x.x.x.js"); 
require("path/to/velocity.js"); 
// Optional: If you're using the UI pack, require it after Velocity. (You don't need to assign it to a variable.) 
require("path/to/velocity.ui.js"); 
/* Your app code here. */ 
$("body").velocity({ opacity: 0.5 }); 

Вы назначены только Jquery к локальной$ переменной в .

Смежные вопросы