在5.7中将npm软件包安装到Laravel

时间:2019-04-09 13:19:04

标签: javascript laravel npm

我正在尝试将npm软件包安装到我的Laravel项目中。因此,我用npm install安装了npm,然后进行了npm install masonry-layout,然后运行了npm run watch,它出现在我的node_modules文件夹中。

我尝试将require('masonry-layout');添加到我的app.js中,并将window.anything = require('masonry-layout');window._ = require('masonry-layout');添加到我的bootstrap.js中-我在这样的视图中调用它:

var $grid = $('.grid').imagesLoaded( function() {
    $grid.masonry({
        itemSelector: '.grid-item',
        columnWidth: '.grid-sizer',
        percentPosition: true,
        isResizable: true,
        transitionDuration: '0.8s',
        isAnimated: true
    });
});

我的app.js:

require('./bootstrap');
require('masonry-layout');
require('imagesloaded');

bootstrap.js:


window._ = require('lodash');
window.anything = require('masonry-layout');
window.anything = require('imagesloaded');


/**
 * We'll load jQuery and the Bootstrap jQuery plugin which provides support
 * for JavaScript based Bootstrap features such as modals and tabs. This
 * code may be modified to fit the specific needs of your application.
 */

try {
    window.Popper = require('popper.js').default;
    window.$ = window.jQuery = require('jquery');

    require('bootstrap');
} catch (e) {}

/**
 * We'll load the axios HTTP library which allows us to easily issue requests
 * to our Laravel back-end. This library automatically handles sending the
 * CSRF token as a header based on the value of the "XSRF" token cookie.
 */

window.axios = require('axios');

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

/**
 * Next we will register the CSRF Token as a common header with Axios so that
 * all outgoing HTTP requests automatically have it attached. This is just
 * a simple convenience so we don't have to attach every token manually.
 */

let token = document.head.querySelector('meta[name="csrf-token"]');

if (token) {
    window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
    console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}

/**
 * Echo exposes an expressive API for subscribing to channels and listening
 * for events that are broadcast by Laravel. Echo and event broadcasting
 * allows your team to easily build robust real-time web applications.
 */

// import Echo from 'laravel-echo'

// window.Pusher = require('pusher-js');

// window.Echo = new Echo({
//     broadcaster: 'pusher',
//     key: process.env.MIX_PUSHER_APP_KEY,
//     cluster: process.env.MIX_PUSHER_APP_CLUSTER,
//     encrypted: true
// });

是的,我也安装了imagesLoaded,但这也不起作用。如果我将CDN包含在我的视图中,它将像应该的那样工作。

我在做什么错了?

1 个答案:

答案 0 :(得分:2)

您正在加载masonry-layout之前先加载imagesLoadedjQuery。您应该像这样在jQuery之后加载它:

try {
    window.Popper = require('popper.js').default;
    window.$ = window.jQuery = require('jquery');

    window.Masonry = require('masonry-layout');
    window.ImagesLoaded = require('imagesloaded');

    require('bootstrap');
} catch (e) {}

然后您可以像这样使用它:

new Masonry('.grid', {
    // options
});

此外,您可以将它们从app.js中删除。

注意

如果您真的想像$('.grid').masonry(...)一样使用它,则需要安装jquery-bridget

npm install jquery-bridget

然后在您的引导文件中执行此操作:

var $ = require('jquery');
var jQueryBridget = require('jquery-bridget');
var Masonry = require('masonry-layout');

// make Masonry a jQuery plugin
jQueryBridget( 'masonry', Masonry, $ );

// now you can use $().masonry()
$('.grid').masonry({
  columnWidth: 80
});

有关更多信息:https://masonry.desandro.com/extras.html#webpack