Vue項(xiàng)目中使用Bootstrap
一、安裝jQuery
Bootstrap需要依賴jQuery,所以引用Bootstrap之前要先引用jQuery,使用下面的命令引用jQuery:
npm install jquery --save
注意:如果想查看npm上jquery有哪些版本,可以執(zhí)行命令:
npm view jquery versions

二、引入Bootstrap
1、使用命令安裝
可以使用下面的命令安裝:
npm install bootstrap --save

2、下載Bootstrap文件
直接去Bootstrap下載bootstrap包,把下載好的文件放到src/assets目錄下面:

三、配置使用jQuery
1、添加webpack
在build/webpack.base.conf.js文件的頂部添加下面的代碼:
const webpack=require("webpack")2、修改webpack.base.conf.js
在build/webpack.base.conf.js文件中,在整個(gè)配置對(duì)象的末尾增加plugins配置:
// 配置全局使用 jquery
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
jquery: "jquery",
"window.jQuery": "jquery"
})]build/webpack.base.conf.js完整代碼如下:
'use strict'
const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf')
const webpack=require("webpack")
function resolve (dir) {
return path.join(__dirname, '..', dir)
}
// const createLintingRule = () => ({
// test: /\.(js|vue)$/,
// loader: 'eslint-loader',
// enforce: 'pre',
// include: [resolve('src'), resolve('test')],
// options: {
// formatter: require('eslint-friendly-formatter'),
// emitWarning: !config.dev.showEslintErrorsInOverlay
// }
// })
module.exports = {
context: path.resolve(__dirname, '../'),
entry: {
app: './src/main.js'
},
output: {
path: config.build.assetsRoot,
filename: '[name].js',
publicPath: process.env.NODE_ENV === 'production'
? config.build.assetsPublicPath
: config.dev.assetsPublicPath
},
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': resolve('src')
}
},
module: {
rules: [
// 關(guān)閉eslint編碼規(guī)范驗(yàn)證
// ...(config.dev.useEslint ? [createLintingRule()] : []),
{
test: /\.vue$/,
loader: 'vue-loader',
options: vueLoaderConfig
},
{
test: /\.js$/,
loader: 'babel-loader',
include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('media/[name].[hash:7].[ext]')
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
}
}
]
},
node: {
// prevent webpack from injecting useless setImmediate polyfill because Vue
// source contains it (although only uses it if it's native).
setImmediate: false,
// prevent webpack from injecting mocks to Node native modules
// that does not make sense for the client
dgram: 'empty',
fs: 'empty',
net: 'empty',
tls: 'empty',
child_process: 'empty'
},
// 配置全局使用 jquery
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
jquery: "jquery",
"window.jQuery": "jquery"
})]
}四、在main.js中引用
// 引入jQuery import $ from 'jquery' // 引用bootstrap import './assets/bootstrap-3.3.7-dist/css/bootstrap.css' import './assets/bootstrap-3.3.7-dist/js/bootstrap.js'
五、測(cè)試
<template>
<div>
<p class="col-md-6">
<button class="btn btn-primary">測(cè)試按鈕</button>
</p>
</div>
</template>
<script>
export default {
name:"test",
}
</script>運(yùn)行效果:

到此這篇關(guān)于Vue項(xiàng)目中使用Bootstrap的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用vue寫(xiě)一個(gè)翻頁(yè)的時(shí)間插件實(shí)例代碼
最近在做自己項(xiàng)目中遇到一個(gè)非常簡(jiǎn)單的功能,跟大家分享下,這篇文章主要給大家介紹了關(guān)于使用vue寫(xiě)一個(gè)翻頁(yè)的時(shí)間插件的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
vue多語(yǔ)言轉(zhuǎn)換的幾種方法總結(jié)
這篇文章主要介紹了vue多語(yǔ)言轉(zhuǎn)換的幾種方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
Vue-cli搭建spa項(xiàng)目的項(xiàng)目實(shí)踐
本文主要介紹了Vue-cli搭建spa項(xiàng)目的項(xiàng)目實(shí)踐,首先,你需要安裝Vue CLI,然后通過(guò)它創(chuàng)建新項(xiàng)目,接著,選擇和配置適當(dāng)?shù)牟寮鸵蕾図?xiàng),以完善你的SPA項(xiàng)目,感興趣的可以了解一下2023-09-09
Element-ui el-tree新增和刪除節(jié)點(diǎn)后如何刷新tree的實(shí)例
這篇文章主要介紹了Element-ui el-tree新增和刪除節(jié)點(diǎn)后如何刷新tree的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
Vue3?KeepAlive實(shí)現(xiàn)原理解析
KeepAlive?是一個(gè)內(nèi)置組件,那封裝一個(gè)組件對(duì)于大家來(lái)說(shuō)應(yīng)該不會(huì)有太大的困難,它的核心邏輯在于它的?render?函數(shù),它用?map?去記錄要緩存的組件,就是?[key,vnode]?的形式,這篇文章主要介紹了Vue3?KeepAlive實(shí)現(xiàn)原理,需要的朋友可以參考下2022-09-09
vue實(shí)現(xiàn)簡(jiǎn)單實(shí)時(shí)匯率計(jì)算功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)單實(shí)時(shí)匯率計(jì)算功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
vue-cli中實(shí)現(xiàn)響應(yīng)式布局的方法
這篇文章主要介紹了vue-cli中實(shí)現(xiàn)響應(yīng)式布局的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

