search

Home  >  Q&A  >  body text

How to import a package in laravel after installing it with npm?

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?

曾经蜡笔没有小新曾经蜡笔没有小新2755 days ago819

reply all(3)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-05-16 16:48:29

    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

    reply
    0
  • ringa_lee

    ringa_lee2017-05-16 16:48:29

    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

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-16 16:48:29

    Insert it directly at the bottom of the page using the <script> tag

    reply
    0
  • Cancelreply