How to import a package in laravel after installing it with npm?
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-05-16 16:46:29
0
3
827

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?

曾经蜡笔没有小新
曾经蜡笔没有小新

reply all(3)
伊谢尔伦
window.sweetalert2 = require('sweetalert2')

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template