admin管理员组文章数量:1432752
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?
1 Answer
Reset to default 7You 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!
本文标签: javascriptHow to require npm packages after installing it in LaravelStack Overflow
版权声明:本文标题:javascript - How to require npm packages after installing it in Laravel? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745585130a2664855.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论