VUE的接口獲取swiper輪播阻星、安裝使用less

1.VUE的接口獲取swiper輪播:

在public文件下創(chuàng)建一個data文件夾再創(chuàng)建imgJson.json文件:

{

? ? "imglist":[

? ? ? ? {

? ? ? ? "imgurl":"https://img2.baidu.com/it/u=2741764822,31952901&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500",

? ? ? ? ? "url":"www.baidu.com"

? ? ? ? },

? ? ? ? {

? ? ? ? "imgurl":"https://img0.baidu.com/it/u=2531828205,2557062548&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=281",

? ? ? ? ? "url":"www.baidu.com"

? ? ? ? },

? ? ? ? {

? ? ? ? ? ? "imgurl":"https://img0.baidu.com/it/u=2752337540,3600841572&fm=253&fmt=auto&app=138&f=JPEG?w=1058&h=500",

? ? ? ? ? "url":"www.baidu.com"

? ? ? ? },

? ? ? ? {

? ? ? ? ? ? "imgurl":"https://img1.baidu.com/it/u=1054244600,3783921739&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=250",

? ? ? ? ? ? "url":"www.baidu.com"

? ? ? ? }

? ? ]

}

main.js里全局引用一下:

安裝教程在上篇

import Vue from 'vue'

import App from './App.vue'

import router from './router'

import store from './store'

import VueAwesomeSwiper from 'vue-awesome-swiper'

import 'swiper/css/swiper.css'

/* 導(dǎo)入less */

import less from 'less'

/* 把less當(dāng)作組件引入 */

Vue.use(less)

Vue.config.productionTip = false

Vue.use(VueAwesomeSwiper)

new Vue({

? router,

? store,

? render: h => h(App)

}).$mount('#app')

components下創(chuàng)建一個輪播組件:

<template>

? <div id="app">

? ? <!-- 因為在main.js中全局引入過了盖腕,所以組件可以直接拿來用 -->

? ? <swiper ref="mySwiper" :options="swiperOptions" v-if="imgList.length">

? ? ? <!-- ?@click.native 如果組件使用點擊事件無效 可以使用修飾符.native 轉(zhuǎn)成原生事件 -->

? ? ? <swiper-slide

? ? ? ? v-for="(v, i) in imgList"

? ? ? ? :key="i"

? ? ? ? @click.native="goto(v.url)"

? ? ? >

? ? ? ? <img :src="v.imgurl" width="100%" height="100%" />

? ? ? </swiper-slide>

? ? ? <div class="swiper-pagination" slot="pagination"></div>

? ? </swiper>

? </div>

</template>

<script>

import axios from "axios";

export default {

? name: "App",

? created: function () {

? ? /* 數(shù)據(jù)是異步的嚣艇, 數(shù)據(jù)還沒有到情況下亭饵,輪播圖組件已經(jīng)開始加載了赠涮,

? ? 導(dǎo)致配置無縫輪播的時候效果出不來 怎么辦子寓?*/

? ? /* 解決方法:使用條件判斷,當(dāng)數(shù)據(jù)還沒有獲取到的時候不加載輪播圖笋除,

? ? 數(shù)據(jù)到了斜友,再加載 */


? ? axios.get("/data/imgJson.json").then((res) => {

? ? ? this.imgList = res.data.imglist;

? ? ? /* 使用refs的方法 必須要配置$nextTick獲取到dom之后再執(zhí)行slideTo方法 */

? ? ? /* 在這里使用$nextTick方法 是因為組件是后來有數(shù)據(jù)的時候加載上去的,

? ? ? 擔(dān)當(dāng)于更新了dom的值垃它,這時候想獲取dom就必須借助于$nextTick方法 */

? ? ? ? ?/* 在異步操作里面slideTo第一個參數(shù)表示第幾張 ?*/

? ? ? this.$nextTick(()=>{


? ? ? ? this.$refs.mySwiper.swiper.slideTo(2,1000,false)

? ? ? })

? ? });

? },

? methods: {

? ? goto: function (url) {

? ? ? /* console.log(url) */

? ? ? /* window.open會打開一個新的窗口 */

? ? ? window.open(url);

? ? ? /* location.href在當(dāng)前頁跳轉(zhuǎn) */

? ? ? /* location.href = url; */

? ? },

? },

? data() {

? ? return {

? ? ? imgList: [],

? ? ? swiperOptions: {


? ? ? ? effect:'fade',/* cube */

? ? ? ? pagination: {

? ? ? ? ? el: ".swiper-pagination",/* notNextTick: true, */

? ? ? ? },

? ? ? ? loop:true,

? ? ? ? autoplay: {

? ? ? ? ? delay: 1000,

? ? ? ? ? /* 如果設(shè)置為true鲜屏,當(dāng)切換到最后一個slide時停止自動切換。(loop模式下無效)嗤瞎。 */

? ? ? ? ? stopOnLastSlide: false,

? ? ? ? ? /* 如果設(shè)置為false墙歪,用戶操作swiper之后自動切換不會停止,每次都會重新啟動autoplay贝奇。 */

? ? ? ? ? disableOnInteraction: false,

? ? ? ? },

? ? ? },

? ? };

? },


? mounted() {


? ? /* console.log("Current Swiper instance object", this.$refs.mySwiper.swiper); */

? ? /* this.swiper.slideTo(3, 1000, true); */


? ? // console.log(this.$refs.mySwiper.swiper.slideTo(1,1000,false) )

? },

};

</script>

<style scoped>

/* scoped 會防止組件內(nèi)的樣式污染全局 也會優(yōu)先使用組件內(nèi)的樣式 */

.swiper-container {

? width: 700px;

? height: 500px;

? border: 1px solid red;

}

.red{

? color: red;

}

</style>

App.vue中引入輪播組件:

<template>

? <div id="app">


? ? <h1 class="red">輪播圖</h1>

? ? <my-swiper />


? </div>

</template>

效果圖:

2.安裝使用less:

1虹菲、npm i less --save-dev 把less源碼安裝到開發(fā)環(huán)境

/* less文件是通過less.loader.js 來編譯成css最后加載到頁面中的 */

2、npm i less-loader@6 --save-dev 安裝less解析器 (★一定要指定版本)

3掉瞳、lessc -v 查看版本

4毕源、在main.js? import less from 'less'? Vue.use(less)

5浪漠、獨立的vue文件需要引入less <style lang="less"></style>

實際應(yīng)用:

App.vue文件:

<template>

? <div id="app">


? ? <h1 class="red">輪播圖</h1>

? ? <my-swiper />

? ? <div class="box">

? ?<h1>歡迎使用less</h1>

? ? </div>

? ? <div class="box1">

? ? <div class="box2">

? ? ? ? <div class="box3">

? ? </div>

? ? </div>

</div>

<ul>

? <li v-for="(v,i) in 4" :key="i">{{v}}</li>

</ul>

<div class="a1">我是a1</div>

<div class="a2">我是a2</div>

? </div>

</template>

<script>

/* 這樣也能引入 */

/* import './less/common.less' */

import MySwiper from '@/components/MySwiper.vue'

export default{

? components:{

? ? MySwiper

? }

}

</script>

<style scopd lang="less">

/* 使用導(dǎo)入式引入樣式庫 */

@import url(./less/common.less);

</style>

src文件下創(chuàng)建less文件夾,創(chuàng)建兩個less文件:

common.less文件:

/* 可以在less中引入別的less文件 從而提高代碼復(fù)用 */

@import url(./init.less);

/* 定義一個函數(shù) */

.test(@color:red,@size:14px){

? background: @color;

? font-size: @size;

}

.a1{

? .test()

}

.a2{

? .test(@color:@colorGreen,@size:30px)

}

ul{

? width: @k;

? height: @k;

? background: @colorRed;

}

li:nth-of-type(1){

? /* 加減法的時候左右一定要空格,否則會理解為橫杠- */

? width: @k - 20px;

? background: @colorGreen;

}

.box1{

? width: @k*2;

? height: @k*2;

? background: @colorRed;

? .box2{

? ? width: @k;

? height: @k;

? background:@colorGreen;

? ? .box3{

? ? ?width: @k/2;

? height: @k/2;

? background: @colorBlue;

? ? }

? }

}

.box{

? width: 200px;

? height: 200px;

? border: 1px solid red;

? ?/* ?url里面必須要用引號 */

? background: url("@{imgurl}logo.png") no-repeat;

? h1{

? color: @colorRed;

}

}

init.less文件:

*{margin: 0;padding: 0;}

/* 使用變量 可以嵌套 圖片路徑也可以使用變量*/

@colorRed:red;

@colorGreen:green;

@colorBlue:blue;

@imgurl:'../assets/';

@k:100px;

效果圖:


★☆使用手冊:

1霎褐、npm i less --save-dev 把less源碼安裝到開發(fā)環(huán)境

/* less文件是通過less.loader.js 來編譯成css最后加載到頁面中的 */

2址愿、npm i less-loader@6 --save-dev 安裝less解析器 (★一定要指定版本)

3、lessc -v 查看版本

4冻璃、在main.js? import less from 'less'? Vue.use(less)

5响谓、獨立的vue文件需要引入less <style lang="less"></style>

less中變量的使用 定義方式:@key:value; 使用方式:@key;

字符串拼接變量使用方式 @img:'./img/'; background:url("@{img}1.png")

寫減法的時候左右要加空格,否則會理解為杠-

多層嵌套+變量計算省艳;

<div class="box1">

? ? <div class="box2">

? ? ? ? <div class="box3"></div>

? ? </div>

</div>

<style lang="less">

@k:100px;

.box1{

? ? width: @k;

? ? height:@k;

? ? background: red;

? ? .box2{

? ? ? ? width: @k/2;

? ? ? ? height:@k/2;

? ? ? ? background: green;

? ? ? ? .box3{

? ? ? ? ? ? width: @k/3;

? ? ? ? ? ? height:@k/3;

? ? ? ? ? ? background: blue;

? ? ? ? }

? ? }

}

</style>

混合 = 函數(shù)

<div class="box1">我是box1</div>

<div class="box2">我是box2</div>

<style lang="less">

//定義一個函數(shù)娘纷;

.test(@color:red,@size:14px){

? ? background: @color;

? ? font-size:@size;

}

.box1{

//? 不傳參,使用默認的跋炕;

? ? .test()

}

.box2{

//? 給函數(shù)傳參赖晶;

? ? .test(@color:green,@size:30px)

}

</style>

運算符

可以對高度、寬度辐烂、角度進行計算遏插;

<ul>

? ? <li v-for="item in 4">{{item}}</li>

</ul>

<style lang="less" scoped>

? @k:10px;

? ? ul{

? ? ? ? list-style: none;

? ? ? ? ? li{

? ? ? ? ? ? ? border:1px solid ;

? ? ? ? ? ? ? margin:10px 0 ;

? ? ? ? ? }

? ? ? ? ? ? li:nth-child(1){

? ? ? ? ? ? ? ? width: @k + @k;

? ? ? ? ? ? ? ? height:@k;

? ? ? ? ? ? }

? ? ? ? ? ? li:nth-child(2){

? ? ? ? ? ? ? ? width: @k -5px;

? ? ? ? ? ? ? ? height:@k;

? ? ? ? ? ? }

? ? ? ? ? ? li:nth-child(3){

? ? ? ? ? ? ? ? width: @k * @k;

? ? ? ? ? ? ? ? height:@k;

? ? ? ? ? ? }

? ? ? ? ? ? li:nth-child(4){

? ? ? ? ? ? ? ? width: @k / 2;;

? ? ? ? ? ? ? ? height:@k;

? ? ? ? ? ? }

? ? }

</style>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市纠修,隨后出現(xiàn)的幾起案子胳嘲,更是在濱河造成了極大的恐慌,老刑警劉巖分瘾,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件胎围,死亡現(xiàn)場離奇詭異,居然都是意外死亡德召,警方通過查閱死者的電腦和手機白魂,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來上岗,“玉大人福荸,你說我怎么就攤上這事‰戎溃” “怎么了敬锐?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長呆瞻。 經(jīng)常有香客問我台夺,道長,這世上最難降的妖魔是什么痴脾? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任颤介,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘滚朵。我一直安慰自己冤灾,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布辕近。 她就那樣靜靜地躺著韵吨,像睡著了一般。 火紅的嫁衣襯著肌膚如雪移宅。 梳的紋絲不亂的頭發(fā)上归粉,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天,我揣著相機與錄音吞杭,去河邊找鬼盏浇。 笑死,一個胖子當(dāng)著我的面吹牛芽狗,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播痒蓬,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼童擎,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了攻晒?” 一聲冷哼從身側(cè)響起顾复,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎鲁捏,沒想到半個月后芯砸,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡给梅,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年假丧,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片动羽。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡包帚,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出运吓,到底是詐尸還是另有隱情渴邦,我是刑警寧澤,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布拘哨,位于F島的核電站谋梭,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏倦青。R本人自食惡果不足惜瓮床,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧纤垂,春花似錦矾策、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至吼鱼,卻和暖如春蓬豁,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背菇肃。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工地粪, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人琐谤。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓蟆技,卻偏偏與公主長得像,于是被迫代替她去往敵國和親斗忌。 傳聞我的和親對象是個殘疾皇子质礼,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容