How to introduce laravel after installing a package with npm?
For example, if I want to use sweetalert2, install it first:
npm install --save sweetalert2
The installation is complete.
Does it need to be introduced in laravel's \resources\assets\js\bootstrap.js
file after installation? Its 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.common['X-CSRF-TOKEN'] = window.Laravel.csrfToken;
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
The above content seems to have introduced lodash, jquery, bootstrap, and axios, but their writing methods are different, respectively:
window._ = require('lodash');
window.$ = window.jQuery = require('jquery');
require('bootstrap');
window.axios = require('axios');
Question:
1. Now I want to introduce sweetalert2
, how should I write it?
2. Can you explain what the above four ways of writing mean?
Introduce this package, but this package defaults to a local module
If you need to use it globally, attach it to the window global scope
window._ = require('lodash'); Global introduction
window.$ = window.jQuery = require('jquery'); Global introduction
require('bootstrap'); //bootstrap depends on window.$
If you want to know anything, you can take a look at the cmd amd specification in front-end engineering
Insert it directly at the bottom of the page using the
<script>
tag