laravel用npm安裝套件之後要如何引入?
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-05-16 16:46:29
0
3
767

laravel用npm安裝一個套件之後如何引入?

比如,我要使用sweetalert2,先安裝:

npm install --save sweetalert2

安裝完成。

安裝後需要在laravel的\resources\assets\js\bootstrap.js檔案中引入嗎?它的預設內容是這樣的:

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';

上面內容看起來好像是引入了lodash,jquery,bootstrap,axios,但它們的寫法不同,分別是:

window._ = require('lodash');
window.$ = window.jQuery = require('jquery');
require('bootstrap');
window.axios = require('axios');

問題:
1、現在我要引進sweetalert2,該怎麼寫?
2、能否解釋一下上面4種寫法分別是什麼意思?

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

全部回覆(3)
伊谢尔伦
window.sweetalert2 = require('sweetalert2')

引入這個包,但是這個包預設作為局部模組

如果需要全域使用,就把它附加到window全域作用域上

左手右手慢动作

window._ = require('lodash'); 全局引入
window.$ = window.jQuery = require('jquery'); 全局引入
require('bootstrap'); //bootstrap 依賴 window.$

如果你想知道什麼,可以看看前端工程中的 cmd amd 規範

世界只因有你

直接頁底部用 <script> 標籤插進來

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!