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

How to require npm packages after installing it in Laravel? For example,I need package sweetalert2,in

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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745585131a4634489.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信