第一步、在項(xiàng)目中npm安裝JQ
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)制作、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的寶山網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
npm install jquery --save
第二步、檢查是否安裝成功
? ? ? ? ? ?在package.json中的dependencies查看是否含有jquery
第三步、配置JQ
? ? ? ? ? ?在vue.config.js中頂部寫入?const webpack = require('webpack')
? ? ? ? ? ?寫入后在configureWebpack中加入
第四步、重啟項(xiàng)目,引入成功
一、主體不同
1、vue.js:是一套用于構(gòu)建用戶界面的漸進(jìn)式JavaScript框架。
2、jquery:是一個(gè)快速、簡(jiǎn)潔的JavaScript框架,是繼Prototype之后又一個(gè)優(yōu)秀的JavaScript代碼庫(kù)。
二、特點(diǎn)不同
1、vue.js:Vue 被設(shè)計(jì)為可以自底向上逐層應(yīng)用。Vue 的核心庫(kù)只關(guān)注視圖層,方便與第三方庫(kù)或既有項(xiàng)目整合。
2、jquery:具有獨(dú)特的鏈?zhǔn)秸Z法和短小清晰的多功能接口;具有高效靈活的css選擇器,并且可對(duì)CSS選擇器進(jìn)行擴(kuò)展;擁有便捷的插件擴(kuò)展機(jī)制和豐富的插件。
三、優(yōu)勢(shì)不同
1、vue.js:目標(biāo)是通過盡可能簡(jiǎn)單的 API 實(shí)現(xiàn)響應(yīng)的數(shù)據(jù)綁定和組合的視圖組件。
2、jquery:提供了對(duì)基本JavaScript結(jié)構(gòu)的增強(qiáng),比如元素迭代和數(shù)組處理等操作。
參考資料來源:百度百科-jQuery
參考資料來源:百度百科-Vue.js
在Vue.js中使用jquery插件需要利用ProvidePlugin導(dǎo)入jquery全局庫(kù)。1、在build/webpack.dev.conf.js和build/webpack.prod.conf.js中配置即可。2、在模塊中使用的時(shí)候代碼如下:plugins:[//這里是需要導(dǎo)入的插件列表,定意思jquery為全局參數(shù)newwebpack.ProvidePlugin({$:'jquery',jquery:'jquery','window.jQuery':'jquery',jQuery:'jquery'})]也可以使用import的這種寫法:importjQueryfrom'jQuery'ready:function(){varself=this;jQuery(window).resize(function(){self.$refs.thisherechart.drawChart();})},
最近學(xué)習(xí)vue,習(xí)慣性的通過script標(biāo)簽引入jquery,寫完后報(bào)錯(cuò)才想起來,這種方式在vue是不適用的。
1:因?yàn)橐呀?jīng)安裝了vue腳手架,所以需要在webpack中全局引入jquery
打開package.json文件,在里面加入這行代碼,jquery后面的是版本,根據(jù)你自己需求更改。
dependencies:{
"jquery":"^2.2.3"
}
然后在命令行中cnpm install
大多人應(yīng)該都是使用的淘寶鏡像,所以使用cnpm,如果你不是 ,可以使用npm安裝。
2:在webpack.base.conf.js中加入一行代碼
var webpack=require("webpack")
3:在webpack.base.conf.js中module.exports的最后加入這行代碼,
plugins: [
new webpack.optimize.CommonsChunkPlugin('common.js'),
new webpack.ProvidePlugin({
jQuery: "jquery",
$: "jquery"
})
]
4:在main.js中引入,加入下面這行代碼
import $ from 'jquery'
5:最后一步,重新跑一邊就好,cnpm run dev
安裝 jQuery 和 cropper.js
# install jQuery cropper
$ npm install jquery cropper --save
為jquery和Vue自定義指令配置webpack
為webpack配置添加jquery和Vue自定義指令的映射。
通常webpack已經(jīng)引入了完整的jquery版本,但還是建議再一次引入一下。
您可以看到Vue的webpack模板已經(jīng)添加到組件的文件夾中。我通常會(huì)添加很多其他文件夾像自定義指令,mixin等等。在這個(gè)例子中,我們只添加了自定義指令。
這將幫助我們引入依賴關(guān)系而無需知道其確切的路徑。這也是有益的在你重構(gòu)你的應(yīng)用的時(shí)候。你也并不需要管理相對(duì)路徑。
把下面高亮部分添加到build/webpack.base.conf文件中。
resolve: {
extensions: ['', '.js', '.vue'],
fallback: [path.join(__dirname, '../node_modules')],
alias: {
'src': path.resolve(__dirname, '../src'),
'assets': path.resolve(__dirname, '../src/assets'),
'components': path.resolve(__dirname, '../src/components'),
'jquery': path.resolve(__dirname, '../node_modules/jquery/src/jquery'),
'directives': path.resolve(__dirname, '../src/directives')
}
},
一 .引入swiper(全局,局部)
方法一:全局引入,也是最暴力的,但是也是有好處壞處(同時(shí)加載,但是不能保證同時(shí)下載)
link href="" rel="stylesheet"script src=""/script12
組件中可以直接使用的swiper了
_initSwiper() { ? ? ? ?const container = this.$refs.swiper; ? ? ? ?const config = {
? ?effect: 'coverflow',
? ?slidesPerView: 'auto',
? ?centeredSlides: true,
? ?initialSlide: this.activeIndex,
? ?loop: true,
? ?autoplay: 1000,
? ?speed: 1000,
? ?coverflow: {
? ? ?rotate: 0,
? ? ?stretch: -30,
? ? ?depth: 100,
? ? ?modifier: 0.7,
? ? ?slideShadows: false,
? ?},
?}; ? ?this.mySwiper = new Swiper(container, config);
}1234567891011121314151617181920
2.方法二:main.js 中
import '../node_modules/swiper/dist/css/swiper.min.css';import 'swiper';12
執(zhí)行上面的_initSwiper()的方法 即可
3.方法三:局部的引入的,有時(shí)只想的單個(gè)組件中使用某一個(gè)的庫(kù),方法如下
section ref="swiper" class="swiper-container"
div class="swiper-wrapper"
div class="demo swiper-slide" v-for="item in colorList" :style="`backgroundColor:${item}`"/div
/div/sectionscript
let swiperAsync = import('swiper') //引入的swiper.js(node_modules)的方法
export default {
data(){ ? ? ?return {
?colorList: ['red', 'yellow', 'gray', 'pink']
}
},
methods: {
async _initSwiper() { ? ? ? ?let Swiper = await swiperAsync; //異步加載的
?const container = this.$refs.swiper; //ref='swiper'
?const config = { ?//swiper的參數(shù)配置
? ?effect: 'coverflow',
? ?slidesPerView: 'auto',
? ?centeredSlides: true,
? ?initialSlide: this.activeIndex,
? ?loop: true,
? ?autoplay: 1000,
? ?speed: 1000,
? ?coverflow: {
? ? ?rotate: 0,
? ? ?stretch: -30,
? ? ?depth: 100,
? ? ?modifier: 0.7,
? ? ?slideShadows: false,
? ?},
?}; ? ? ? ?this.mySwiper = new Swiper(container, config);
},
},
mounted(){ ? ? ?this._initSwiper();
}
}/scriptstyle lang="scss" scoped
/*@import '../assets/styles/swiper.min.css'; !*靜態(tài)資源的文件*!*/
@import '../../node_modules/swiper/dist/css/swiper.min.css';/style12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
二 引入的jquery的方法
全局的方法
script src=""/script1
組件中可以直接使用的 ‘$’的方法
局部的方法:
npm install jquery -D1
需要使用的組件中引入
import $ from 'jquery'1
新聞名稱:vue中使用jquery,vue中使用jquery輪播圖插件
文章地址:http://vcdvsql.cn/article12/dsdeddc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、靜態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)、Google、用戶體驗(yàn)、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)