【问题标题】:Installing npm packages to Laravel in 5.7在 5.7 中将 npm 包安装到 Laravel
【发布时间】:2019-08-30 19:28:48
【问题描述】:

我正在尝试将 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 包含在我的视图中,它会像它应该做的那样工作。

我做错了什么?

【问题讨论】:

  • 你能告诉我们你在哪里需要masonry-layout吗?还有,你是在你加载完jQuery之后再做的吗?
  • 我在需要 jQuery 之前和之后都试过了。显示我需要砌体布局的地方是什么意思?我将 app.js 包含在我的视图文件中,我猜应该这样做吗?还是在我看来我也必须要求砌体布局?因为那时我可以只添加 CDN,这不是我想要的。
  • 我想看看你的js文件。您只显示了部分代码。
  • 我的资源/js/app.js 只包含 require('masonry-layout');需要('图片加载');
  • 它没有加载引导文件?

标签: javascript laravel npm


【解决方案1】:

在加载 jQuery 之前,您正在加载 masonry-layout 和 imagesLoaded。你应该像这样在 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

【讨论】:

  • 现在我的 app.js 只包含 require('./bootstrap');我已经像你展示的那样做了,但它仍然无法正常工作。
  • $grid.masonry 不是函数
  • 现在可以工作了。最后!太感谢了。现在,当我想安装更多包时,我只需要在 try 块中调用它们吗?
  • @Jascusi 您可以将它们放在您的 boostrap 文件中的任何位置。但如果它需要 jQuery,请将其放在 try 块中以先加载 jQuery。
猜你喜欢
  • 2019-08-22
  • 2019-02-11
  • 1970-01-01
  • 2020-04-11
  • 1970-01-01
  • 2019-04-03
  • 2019-04-11
  • 2015-07-09
  • 2019-05-14
相关资源
最近更新 更多