前言
最近有小伙伴問道如何在vue-cli項(xiàng)目中引入第三方插件或者庫紊搪,例如如果想在項(xiàng)目中使用jQuery中的Ajax請(qǐng)求數(shù)據(jù)呢蜜葱?或者我想使用Bootstrap框架呢?等等這些問題耀石,本篇博客將帶你學(xué)習(xí)如何引入第三方插件或者庫(僅僅只是一部分牵囤,如果沒有您想要的可以自行百度),那么一起來看看吧滞伟!
本章目標(biāo)
- 學(xué)會(huì)vue-cli項(xiàng)目中引入jQuery
- 學(xué)會(huì)vue-cli項(xiàng)目中引入Bootstrap
vue-cli項(xiàng)目中引入jQuery和Bootstrap
首先我們需要引入的是jQuey這個(gè)庫揭鳞,畢竟作為一名前端開發(fā)人員,我們經(jīng)常使用jQuey中的ajax請(qǐng)求數(shù)據(jù),但是學(xué)完本篇博客你可以使用另一種方法請(qǐng)求數(shù)據(jù)诗良,就是下文提到的axios汹桦,這個(gè)相對(duì)于jQuey中的ajax來說是相對(duì)好用的。
(1)添加依賴并安裝依賴
項(xiàng)目根目錄下找到package.json 添加
"bootstrap": "^3.3.6",
"jquery": "^2.1.4",
版本可以根據(jù)自己的需要修改
安裝命令
cnpm install
npm install
安裝完成之后我們?nèi)ode_modules查看是否安裝成功鉴裹,安裝成功之后的結(jié)果
(2)導(dǎo)入jQuey和Bootstrap
在main.js 導(dǎo)入 (注意導(dǎo)入是node_modules下的路徑可以點(diǎn)進(jìn)去查看具體位置)min是壓縮后文件建議導(dǎo)入這個(gè)
import 'jquery/dist/jquery.min.js'
import 'bootstrap/dist/css/bootstrap.min.css'
import 'bootstrap/dist/js/bootstrap.min.js'
main.js
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuex from 'vuex'
import store from './store/index'
// import router from './router'
// import router from './router/hello'
// import router from './router/test'
// import router from './router/common'
// import router from './router/one'
import router from './router/two'
import 'jquery/dist/jquery.min'
import 'bootstrap/dist/js/bootstrap.min'
import 'bootstrap/dist/css/bootstrap.min.css'
Vue.config.productionTip = false
Vue.use(Vuex)
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
store,
components: {},
template: ''
})
使用這種方法引入
(3)使用內(nèi)置插件ProvidePlugin自動(dòng)加載模塊
此時(shí)jQuery并未依賴成功舞骆,將提示錯(cuò)誤:
需在build/webpack.base.conf.js中增加插件配置
const webpack = require('webpack')
配置中添加
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"windows.jQuery": "jquery"
})
],
build下webpack.base.conf.js的完整結(jié)果
'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)
}
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: [
{
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]')
}
}
]
},
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"windows.jQuery": "jquery"
})
],
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'
}
}
(4)使用jQuery與Bootstrap
Bootstrap資料
Bootstrap中文網(wǎng):https://www.bootcss.com/
菜鳥教程:https://www.runoob.com/bootstrap/bootstrap-tutorial.html
配置好之后我們就直接使用jQuey和Bootstrap
自己可以新建一個(gè)組件中使用jQuery相關(guān)方法和Bootstrap相關(guān)資源
IndexComponent.vue
<template>
<div>
<h1>你好</h1>
<button @click="changeMsg">改變消息</button>
<ul class="nav nav-tabs">
<li role="presentation" class="active"><a href="/">首頁</a></li>
<li role="presentation"><a href="#/a">組件A</a></li>
<li role="presentation"><a href="#/b">組件B</a></li>
<li role="presentation"><a href="#/c">組件C</a></li>
</ul>
</div>
</template>
<script>
export default {
name: "IndexComponent",
data(){
return{
}
},
methods:{
changeMsg(){
$('h1').text('我好')
}
}
}
</script>
<style scoped>
</style>
結(jié)果: