Vue项⽬中使⽤Bootstrap
⼀、安装jQuery
Bootstrap需要依赖jQuery,所以引⽤Bootstrap之前要先引⽤jQuery,使⽤下⾯的命令引⽤jQuery:npm install jquery --save
注意:如果想查看npm上jquery有哪些版本,可以执⾏命令:
npm view jquery versions
⼆、引⼊Bootstrap
1、使⽤命令安装
可以使⽤下⾯的命令安装:
npm install bootstrap --save
2、下载Bootstrap⽂件
直接去Bootstrap下载bootstrap包,把下载好的⽂件放到src/assets⽬录下⾯:
三、配置使⽤jQuery
1、添加webpack
在build/f.js⽂件的顶部添加下⾯的代码:
const webpack=require("webpack")
2、修改f.js
在build/f.js⽂件中,在整个配置对象的末尾增加plugins配置:// 配置全局使⽤ jquery
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
jquery: "jquery",
"window.jQuery": "jquery"
})]
build/f.js完整代码如下:
'use strict'
const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./f')
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
//  }
// })
context: solve(__dirname, '../'),
entry: {
app: './src/main.js'
},
output: {
path: config.build.assetsRoot,
filename: '[name].js',
publicPath: v.NODE_ENV === 'production'
config.build.assetsPublicPath
bootstrap 5
: config.dev.assetsPublicPath
},
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': resolve('src')
}
},
module: {
rules: [
// 关闭eslint编码规范验证
// ...(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'
五、测试
<template>
<div>
<p class="col-md-6">
<button class="btn btn-primary">测试按钮</button>
</p>
</div>
</template>
<script>
export default {
name:"test",
}
</script>
运⾏效果:
到此这篇关于Vue项⽬中使⽤Bootstrap的⽂章就介绍到这了。希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。