【发布时间】: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