最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to require npm packages after installing it in Laravel? - Stack Overflow

programmeradmin0浏览0评论

How to require npm packages after installing it in Laravel?

For example,I need package sweetalert2,installing it first:

npm install --save sweetalert2

Now,do I need to require it in \resources\assets\js\bootstrap.js file in laravel?

It's default content is like this:

window._ = require('lodash');

try {
    window.$ = window.jQuery = require('jquery');

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


window.axios = require('axios');

window.axios.defaults.headersmon['X-CSRF-TOKEN'] = window.Laravel.csrfToken;
window.axios.defaults.headersmon['X-Requested-With'] = 'XMLHttpRequest';

It looks like lodash,jquery,bootstrap,axios have been required into laravel,but the 4 sentences are different,they are like this:

window._ = require('lodash');
window.$ = window.jQuery = require('jquery');
require('bootstrap');
window.axios = require('axios');

Questions:

1、why do the 4 sentences have differences?

2、I want to require package sweetalert2,how do I write it?

How to require npm packages after installing it in Laravel?

For example,I need package sweetalert2,installing it first:

npm install --save sweetalert2

Now,do I need to require it in \resources\assets\js\bootstrap.js file in laravel?

It's default content is like this:

window._ = require('lodash');

try {
    window.$ = window.jQuery = require('jquery');

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


window.axios = require('axios');

window.axios.defaults.headers.mon['X-CSRF-TOKEN'] = window.Laravel.csrfToken;
window.axios.defaults.headers.mon['X-Requested-With'] = 'XMLHttpRequest';

It looks like lodash,jquery,bootstrap,axios have been required into laravel,but the 4 sentences are different,they are like this:

window._ = require('lodash');
window.$ = window.jQuery = require('jquery');
require('bootstrap');
window.axios = require('axios');

Questions:

1、why do the 4 sentences have differences?

2、I want to require package sweetalert2,how do I write it?

Share Improve this question edited May 15, 2017 at 7:35 Suraj Rao 29.6k11 gold badges96 silver badges104 bronze badges asked May 14, 2017 at 17:17 zwl1619zwl1619 4,23216 gold badges63 silver badges114 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You should just be able to require it in your bootstrap.js file:

require('sweetalert2');

Then (assuming you're using scss) in your entry .scss file:

@import "node_modules/sweetalert2/src/sweetalert2";

Also, the reason you don't have to manually attach bootstrap to thewindow is because it will do it automatically when it's loaded. This is because it's assumed the window instance will always exist as it's a styling framework. One of the reason the other libraries might not do this is because they can be used in environments where window isn't defined.

Hope this helps!

发布评论

评论列表(0)

  1. 暂无评论