gpt4 book ai didi

javascript - 在 5.7 中将 npm 包安装到 Laravel

转载 作者:行者123 更新时间:2023-11-28 17:07:03 25 4
gpt4 key购买 nike

我正在尝试将 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 - 我在我的 View 上调用它,如下所示:

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 包含到我的 View 中,它就会像它应该做的那样工作。

我做错了什么?

最佳答案

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

关于javascript - 在 5.7 中将 npm 包安装到 Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55593906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com