requireJS with JQuery, Masonry, & ImagesLoaded: Object [object Object] has no method 'imagesLoaded'

前端 未结 5 2208
春和景丽
春和景丽 2021-02-09 16:19

RequireJS newbie here. Trying to convert some JQuery code I had working fine in the old way to work w/ RequireJS.

Header of my page loads three JS files via script tags

5条回答
  •  醉梦人生
    2021-02-09 17:07

    Try using jquery-bridget: https://github.com/desandro/jquery-bridget to convert masonry to a jquery plugin. I created a new js file that gets loaded by requirejs to ensure it is converted before the application starts running:

    //masonry-config.js:
    'use strict'
    
    define(['jquery-bridget', 'masonry'], function(Bridget, Masonry){
        Bridget('masonry', Masonry );
    });   
    

    Then in my requirejs file

    //main.js
    require.config({
       paths: {
           'masonry-config': 'masonry-config'
           .....
       },
       shim: {  
           'angular-masonry': ['angular', 'masonry'],
           'angular' : {
             deps: ['imagesloaded', 'masonry-config'],
             exports: 'angular'
           },
           'masonry': ['imagesloaded'],
       } 
    }
    

    This is for my app using angular and angular-masonry (with masonry) so you might need to config a little differently but hopefully that give you some idea.

提交回复
热议问题