简体   繁体   English

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

[英]Installing npm packages to Laravel in 5.7

I'm trying to install npm packages to my Laravel project. 我正在尝试将npm软件包安装到我的Laravel项目中。 So I've installed npm with npm install and then did npm install masonry-layout then I ran npm run watch and it appears in my node_modules folder. 所以我先用npm install安装了npm install ,然后又做了npm install masonry-layout然后运行npm run watch ,它出现在我的node_modules文件夹中。

I've tried adding require('masonry-layout'); 我试过添加require('masonry-layout'); to my app.js and adding window.anything = require('masonry-layout'); 到我的app.js并添加window.anything = require('masonry-layout'); or window._ = require('masonry-layout'); window._ = require('masonry-layout'); to my bootstrap.js - im calling it on my view like this: 到我的bootstrap.js-我在我的视图中这样调用它:

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

My app.js: 我的app.js:

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

bootstrap.js: 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
// });

and yes, I installed imagesLoaded too, but this isnt working either. 是的,我也安装了imagesLoaded,但这也不起作用。 If I include the cdn to my view it works like it should do. 如果我将CDN包含在我的视图中,它会像应该的那样工作。

What am I doing wrong? 我究竟做错了什么?

You are loading masonry-layout and imagesLoaded before loading your jQuery . 您正在加载masonry-layoutimagesLoaded然后再加载jQuery You should have it load after jQuery like this: 您应该像这样在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) {}

Then you can use it like this: 然后,您可以像这样使用它:

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

Also, you can remove them from your app.js . 另外,您可以将它们从app.js删除。

Note 注意

If you really want to be able to use it like $('.grid').masonry(...) , then you need to install jquery-bridget : 如果您真的想像$('.grid').masonry(...)这样使用它,则需要安装jquery-bridget

npm install jquery-bridget

Then do this in your bootstrap file: 然后在您的引导文件中执行以下操作:

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
});

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

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

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