Webpack如何引入bootstrap的方法
Bootstrap中是一種事實(shí)上的界面標(biāo)準(zhǔn),標(biāo)準(zhǔn)到現(xiàn)在的網(wǎng)站大量的使用它。如果可以使用webpack引入的bootstrapcss,就可以一個(gè)npm install完成項(xiàng)目的依賴,而不必手工的添加到html內(nèi)。
本來以為在入口文件內(nèi)加一行就行:
import 'bootstrap/dist/css/bootstrapcss'
然后安裝依賴:
npm i bootstrap url url-loader style-loader css-loader --save
實(shí)際上卻不是想象的那么簡單。因?yàn)閏ss文件內(nèi)還引用了很多類型的字體文件和矢量圖文件。要引入它,必須同時(shí)提供css之外的類型的對應(yīng)的loader:
//webpackconfigjs: moduleexports = { entry: { 'js' }, output: { filename: 'bundlejs' }, module: { loaders: [ { test: /\css$/, loader: 'style-loader!css-loader' }, { test: /\eot(\?v=\d+\\d+\\d+)?$/, loader: "file" }, { test: /\(woff|woff2)$/, loader:"url?prefix=font/&limit=5000" }, { test: /\ttf(\?v=\d+\\d+\\d+)?$/, loader: "url?limit=10000&mimetype=application/octet-stream" }, { test: /\svg(\?v=\d+\\d+\\d+)?$/, loader: "url?limit=10000&mimetype=image/svg+xml" } ] } };
我們在html文件內(nèi)使用那么一點(diǎn)點(diǎn)的bootstrap:
// chtml <html> <body> <ul class="nav nav-pills"> <li role="presentation" class="active"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Home</a></li> <li role="presentation"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Profile</a></li> <li role="presentation"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Messages</a></li> </ul> <script type="text/javascript" src="bundlejs"></script> </body> </html>
再次執(zhí)行轉(zhuǎn)譯:
webpack
打開瀏覽器:
open chtml
看到bootstrap那熟悉而太熟悉的界面。
引入jquery
如果需要使用bootstrap的js插件的話,就必須首先引入jquery。引用jquery的一個(gè)方法是使用webpack插件。
首先安裝jquery:
npm i jquery
其次使用插件裝入jquery,方法是修改webpack的配置文件,加入:
plugins: [ new webpackProvidePlugin({ $: "jquery", jQuery: "jquery" }) ]
在入口文件內(nèi)加入代碼來做驗(yàn)證:
$("body")append("<div>hello world</div>")
如果成功,說明jquery加載成功。這樣你就可以在入口js文件內(nèi)加載bootstrapjs了:
import 'bootstrap/dist/js/bootstrapjs'
排除錯(cuò)誤
我確實(shí)在引入bootstrap的時(shí)候,遇到一個(gè)神奇的錯(cuò)誤。在webpack轉(zhuǎn)譯時(shí)報(bào)錯(cuò),css-loader,unknown word樣子的錯(cuò)誤。對webpackconfigjs文件加入一個(gè)include屬性并指向到不存在的目錄即可。
{ test: /\css$/, include: [ pathresolve(__dirname, "not_exist_path") ], loader: "style!css" }
原始的issue在此:https://githubcom/webpack/cs 。我看看看到此答案時(shí)以為是個(gè)玩笑。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js實(shí)現(xiàn)目錄鏈接,內(nèi)容跟著目錄滾動顯示的簡單實(shí)例
下面小編就為大家?guī)硪黄猨s實(shí)現(xiàn)目錄鏈接,內(nèi)容跟著目錄滾動顯示的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10JavaScript如何將時(shí)間戳轉(zhuǎn)化為年月日時(shí)分秒格式
這篇文章主要給大家介紹了關(guān)于JavaScript如何將時(shí)間戳轉(zhuǎn)化為年月日時(shí)分秒格式的相關(guān)資料,在前端的日常工作當(dāng)中,時(shí)間戳的使用也是不少的,有時(shí)后端返回給我們的數(shù)據(jù)是一個(gè)時(shí)間戳,我們需要轉(zhuǎn)換成年月日,時(shí)分秒的形式展示在頁面當(dāng)中,需要的朋友可以參考下2023-11-11webpack實(shí)現(xiàn)靜態(tài)資源緩存的方法
本文主要介紹了webpack實(shí)現(xiàn)靜態(tài)資源緩存的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08el-date-picker?限制開始時(shí)間和結(jié)束時(shí)間的代碼實(shí)現(xiàn)
在Vue.js中使用Element?UI庫的el-date-picker組件時(shí),可以通過設(shè)置picker-options來限制開始時(shí)間和結(jié)束時(shí)間的選擇范圍,下面通過例子介紹el-date-picker?限制開始時(shí)間和結(jié)束時(shí)間的實(shí)現(xiàn),感興趣的朋友一起看看吧2024-08-08