vue2.0實(shí)現(xiàn)選項(xiàng)卡導(dǎo)航效果
本文實(shí)例為大家分享了vue2.0實(shí)現(xiàn)選項(xiàng)卡導(dǎo)航效果的具體代碼,供大家參考,具體內(nèi)容如下
1:背景是一個web端的電商網(wǎng)站,根據(jù)點(diǎn)擊的導(dǎo)航選項(xiàng)卡呈現(xiàn)不同得種類商品,首先這里說下我的路由結(jié)構(gòu)是導(dǎo)航是一個組件,選項(xiàng)卡的內(nèi)容又是單獨(dú)的一個組件。
這是導(dǎo)航組件的內(nèi)容,這里主要通過v-for循環(huán)生成導(dǎo)航,沒什么好說的,需要注意的是,這因?yàn)檫x項(xiàng)卡需要知道用戶所點(diǎn)擊的是哪個模塊,所以在數(shù)據(jù)中有個typeId屬性,這里需要把該屬性值傳給選項(xiàng)卡組件。
<template>
??? ?<ul class="nav">
??? ??? ?<li v-for="nav in navs" :id="nav.typeId" @click="seeClothes($event)" :class="nav.className">
??? ??? ??? ?{{nav.title}}
??? ??? ?</li>
??? ?</ul>
? </header>
</template>
<script>
export default {
? data(){
? ?? ?return{
? ?? ??? ?navs:[
? ?? ??? ??? ?{
? ?? ??? ??? ??? ?title:"Home",
? ?? ??? ??? ??? ?path:'/home/second',
? ?? ??? ??? ??? ?typeId:'1',
? ?? ??? ??? ??? ?className:'currLi'
? ?? ??? ??? ?},
? ?? ??? ??? ?{
? ?? ??? ??? ??? ?title:"男裝",
? ?? ??? ??? ??? ?path:'/home/second',
? ?? ??? ??? ??? ?typeId:'2'
? ?? ??? ??? ?},
? ?? ??? ??? ?{
? ?? ??? ??? ??? ?title:"女裝",
? ?? ??? ??? ??? ?path:'/home/second',
? ?? ??? ??? ??? ?typeId:'3'
? ?? ??? ??? ?},
? ?? ??? ??? ?{
? ?? ??? ??? ??? ?title:"童裝",
? ?? ??? ??? ??? ?path:'/home/second',
? ?? ??? ??? ??? ?typeId:'4'
? ?? ??? ??? ?}
? ?? ??? ?]
? ?? ?}
? },methods:{
? ?? ?seeClothes(event){
? ?? ??? ?var el=event.currentTarget;
? ?? ??? ?var type=el.getAttribute("id");
?? ??? ??? ?var a = [];
?? ??? ??? ?var p = el.parentNode.children;
?? ??? ??? ?for(var i =0,pl= p.length;i<pl;i++) {
?? ??? ??? ??? ?p[i].classList.remove("currLi");
?? ??? ??? ?}
? ?? ??? ?event.currentTarget.classList.add("currLi");
? ?? ??? ?this.$router.push({path:'/home/clothesType',query:{type:type}})
? ?? ?}
? }
}
</script>這里用到的知識點(diǎn)
1:v-for循環(huán)實(shí)現(xiàn)導(dǎo)航的生成
2:在函數(shù)中如何得到當(dāng)前點(diǎn)擊元素的某個屬性值
這里給每個li綁定了點(diǎn)擊事件,該元素綁定的id值是我們所需要傳給選項(xiàng)卡組件的,所以我們在綁定事件的時候需要在click函數(shù)中添加參數(shù),注意看我這里綁定事件的時候是這樣寫的
<li v-for="nav in navs" :id="nav.typeId" @click="seeClothes($event)" :class="nav.className">
??? ??? ??? ?{{nav.title}}
??? ??? ?</li>點(diǎn)擊事件是這樣寫的
seeClothes(event){
? ?? ??? ?var el=event.currentTarget;//這樣是獲取當(dāng)前點(diǎn)擊的元素
? ?? ??? ?var type=el.getAttribute("id");//這樣就可以獲取當(dāng)前元素的id屬性值
?? ??? ??? ?var a = [];//聲明一個空數(shù)組,用來存放當(dāng)前點(diǎn)擊元素的所有的兄弟元素,用來實(shí)現(xiàn)只給當(dāng)前點(diǎn)擊的元素動態(tài)加背景色
?? ??? ??? ?var p = el.parentNode.children;
?? ??? ??? ?for(var i =0,pl= p.length;i<pl;i++) {
?? ??? ??? ??? ?p[i].classList.remove("currLi");
?? ??? ??? ?}
? ?? ??? ?event.currentTarget.classList.add("currLi");//for循環(huán)是實(shí)現(xiàn)給當(dāng)前元素的兄弟元素去除currLi類,實(shí)現(xiàn)只有當(dāng)前點(diǎn)擊元素有背景色
? ?? ??? ?this.$router.push({path:'/home/clothesType',query:{type:type}})//點(diǎn)擊導(dǎo)航路由跳轉(zhuǎn),并且通過query對象將type參數(shù)傳過去
? ?? ?}現(xiàn)在參數(shù)傳過去了,我們看下選項(xiàng)卡組件是怎么寫的
<template>
? <div>
? ?? ?<div class="typeBox">
? ?? ??? ?<ul class="closeType">
? ?? ??? ??? ?<li v-for="item in data" style="margin-bottom: 24px;">
? ?? ??? ??? ??? ?<div class="npBox">
?? ? ??? ??? ??? ??? ?<img :src="'../static/img/'+item.img" class="cDetail">
?? ? ??? ??? ??? ??? ?<div class="npInnerBox">
?? ??? ? ??? ??? ??? ??? ?<p class="name" >{{item.name}}</p>
?? ??? ? ??? ??? ??? ??? ?<p class="price">{{item.price}}</p>
?? ??? ? ??? ??? ??? ??? ?<el-input-number v-model="num1" @change="handleChange" :min="1" :max="10" label="描述文字"></el-input-number>
?? ??? ? ??? ??? ??? ??? ?<button class="addCart" @click="addCart($event)">加入購物車</button>
?? ??? ? ??? ??? ??? ??? ?<span v-bind:id="item.id" style="display: none;"></span>
?? ? ??? ??? ??? ??? ?</div>
?? ? ??? ??? ??? ?</div>
? ?? ??? ??? ?</li>
? ?? ??? ?</ul>
? ?? ?</div>
? </div>
</template>
<script>
export default {
? data(){
? ?? ?return{
? ?? ??? ?type:'',
? ?? ??? ?data:'',
? ?? ??? ?num1: 1,
? ?? ??? ?userCart:[],
? ?? ??? ?currentPage4:'',
? ?? ??? ?pageSize:'',
? ?? ??? ?total:''
? ?? ?}
? },
? methods:{
? ? ?changeData(){
? ? ??? ??? ?this.type=this.$route.query.type;
? ? ??? ??? ??? ?if(this.type=="2"){
?? ? ??? ??? ?this.data=[
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'m1.jpg',
?? ? ??? ??? ??? ?name:'黑白條紋裙',
?? ? ??? ??? ??? ?id:'M1',
?? ? ??? ??? ??? ?price:'100',
?? ? ??? ??? ?},
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'m2.jpg',
?? ? ??? ??? ??? ?name:'水藍(lán)色斑點(diǎn)無袖連衣裙',
?? ? ??? ??? ??? ??? ?id:'M2',
?? ? ??? ??? ??? ?price:'199',
?? ? ??? ??? ?},
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'m3.jpg',
?? ? ??? ??? ??? ?name:'短袖職業(yè)套裝',
?? ? ??? ??? ??? ??? ?id:'M3',
?? ? ??? ??? ??? ?price:'135',
?? ? ??? ??? ?},
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'m4.jpg',?? ?
?? ? ??? ??? ??? ?name:'黑色職業(yè)套裝',
?? ? ??? ??? ??? ??? ?id:'M4',
?? ? ??? ??? ??? ?price:'288',
?? ? ??? ??? ?},
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'m5.jpg',
?? ? ??? ??? ??? ?name:'格子襯衫',
?? ? ??? ??? ??? ??? ?id:'M5',
?? ? ??? ??? ??? ?price:'155',
?? ? ??? ??? ?},
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'m6.jpg',
?? ? ??? ??? ??? ?name:'女性性感短裙',
?? ? ??? ??? ??? ??? ?id:'M6',
?? ? ??? ??? ??? ?price:'300',
?? ? ??? ??? ?}
?? ? ??? ??? ?];
?? ? ??? ?}else if(this.type=="3"){
?? ? ??? ??? ??? ?this.data=[
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'clothes1.jpg',
?? ? ??? ??? ??? ?name:'黑白條紋裙',
?? ? ??? ??? ??? ?id:'W1',
?? ? ??? ??? ??? ?price:'100',
?? ? ??? ??? ?},
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'clothes2.jpg',
?? ? ??? ??? ??? ?name:'水藍(lán)色斑點(diǎn)無袖連衣裙',
?? ? ??? ??? ??? ??? ?id:'W2',
?? ? ??? ??? ??? ?price:'199',
?? ? ??? ??? ?},
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'clothes3.jpg',
?? ? ??? ??? ??? ?name:'短袖職業(yè)套裝',
?? ? ??? ??? ??? ??? ?id:'W3',
?? ? ??? ??? ??? ?price:'135',
?? ? ??? ??? ?},
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'clothes4.jpg',?? ?
?? ? ??? ??? ??? ?name:'黑色職業(yè)套裝',
?? ? ??? ??? ??? ??? ?id:'W4',
?? ? ??? ??? ??? ?price:'288',
?? ? ??? ??? ?},
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'clothes5.jpg',
?? ? ??? ??? ??? ?name:'格子襯衫',
?? ? ??? ??? ??? ??? ?id:'W5',
?? ? ??? ??? ??? ?price:'155',
?? ? ??? ??? ?},
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'clothes6.jpg',
?? ? ??? ??? ??? ?name:'女性性感短裙',
?? ? ??? ??? ??? ??? ?id:'W6',
?? ? ??? ??? ??? ?price:'300',
?? ? ??? ??? ?}
?? ? ??? ??? ?];
?? ? ??? ?}else if(this.type=="性感職業(yè)裝"){
?? ? ??? ??? ??? ?this.data=[
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'clothes1.jpg',
?? ? ??? ??? ??? ?name:'黑白條紋裙',
?? ? ??? ??? ??? ?id:'W1',
?? ? ??? ??? ??? ?price:'100',
?? ? ??? ??? ?},
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'clothes2.jpg',
?? ? ??? ??? ??? ?name:'水藍(lán)色斑點(diǎn)無袖連衣裙',
?? ? ??? ??? ??? ??? ?id:'W2',
?? ? ??? ??? ??? ?price:'199',
?? ? ??? ??? ?},
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'clothes3.jpg',
?? ? ??? ??? ??? ?name:'短袖職業(yè)套裝',
?? ? ??? ??? ??? ??? ?id:'W3',
?? ? ??? ??? ??? ?price:'135',
?? ? ??? ??? ?},
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'clothes4.jpg',?? ?
?? ? ??? ??? ??? ?name:'黑色職業(yè)套裝',
?? ? ??? ??? ??? ??? ?id:'W4',
?? ? ??? ??? ??? ?price:'288',
?? ? ??? ??? ?},
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'clothes5.jpg',
?? ? ??? ??? ??? ?name:'格子襯衫',
?? ? ??? ??? ??? ??? ?id:'W5',
?? ? ??? ??? ??? ?price:'155',
?? ? ??? ??? ?},
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'clothes6.jpg',
?? ? ??? ??? ??? ?name:'女性性感短裙',
?? ? ??? ??? ??? ??? ?id:'W6',
?? ? ??? ??? ??? ?price:'300',
?? ? ??? ??? ?}
?? ? ??? ??? ?];
? ?? ??? ?}else if(this.type==1){
? ?? ??? ??? ??? ?this.data=[
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'m3.jpg',
?? ? ??? ??? ??? ?name:'黑白條紋裙',
?? ? ??? ??? ??? ?id:'M1',
?? ? ??? ??? ??? ?price:'100',
?? ? ??? ??? ?},
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'m4.jpg',?? ?
?? ? ??? ??? ??? ?name:'黑色職業(yè)套裝',
?? ? ??? ??? ??? ??? ?id:'M4',
?? ? ??? ??? ??? ?price:'288',
?? ? ??? ??? ?},
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'m5.jpg',
?? ? ??? ??? ??? ?name:'格子襯衫',
?? ? ??? ??? ??? ??? ?id:'M5',
?? ? ??? ??? ??? ?price:'155',
?? ? ??? ??? ?},
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'clothes4.jpg',?? ?
?? ? ??? ??? ??? ?name:'黑色職業(yè)套裝',
?? ? ??? ??? ??? ??? ?id:'W4',
?? ? ??? ??? ??? ?price:'288',
?? ? ??? ??? ?},
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'clothes5.jpg',
?? ? ??? ??? ??? ?name:'格子襯衫',
?? ? ??? ??? ??? ??? ?id:'W5',
?? ? ??? ??? ??? ?price:'155',
?? ? ??? ??? ?},
?? ? ??? ??? ?{
?? ? ??? ??? ??? ?img:'clothes6.jpg',
?? ? ??? ??? ??? ?name:'女性性感短裙',
?? ? ??? ??? ??? ??? ?id:'W6',
?? ? ??? ??? ??? ?price:'300',
?? ? ??? ??? ?}
?? ? ??? ??? ?];
? ?? ??? ?}
? ? }
? },
? ?created(){
??? ??? ?this.changeData()
? },
? watch:{
? ?? ? "$route": "changeData"
? }
??
}
</script>當(dāng)時就是這里,給我?guī)砹艘恍├_,一開始我是把獲取數(shù)據(jù)的changeData方法寫在了mounted鉤子函數(shù)中,但是這樣的后果是只有在首次路由跳轉(zhuǎn)的時候才會起作用,再次點(diǎn)擊導(dǎo)航的時候數(shù)據(jù)不會改變
why?因?yàn)閙ounted是創(chuàng)建組件頁面元素掛載以后會走里面的方法~想一下,你再次點(diǎn)擊的時候只會路由跳轉(zhuǎn),并不會重新創(chuàng)建組件,所以~
我在想怎么能每次點(diǎn)擊導(dǎo)航路由跳轉(zhuǎn)以后都重新走一次獲取數(shù)據(jù)的changeData方法,我想了想,因?yàn)閷?dǎo)航點(diǎn)擊每次都會進(jìn)行路由跳轉(zhuǎn),所以~我可以在watch里監(jiān)聽路由跳轉(zhuǎn),這樣每次點(diǎn)擊導(dǎo)航的時候都會觸發(fā)監(jiān)聽方法,從而會調(diào)用changeData方法~~解決了不是~~上面的代碼有具體的寫法
最后說下如何監(jiān)聽路由
watch:{
? "$route": "changeData"
}下篇文章說下watch的具體用法
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue router導(dǎo)航守衛(wèi)(router.beforeEach())的使用詳解
- vue實(shí)現(xiàn)導(dǎo)航欄效果(選中狀態(tài)刷新不消失)
- vue 和vue-touch 實(shí)現(xiàn)移動端左右導(dǎo)航效果(仿京東移動站導(dǎo)航)
- vue實(shí)現(xiàn)nav導(dǎo)航欄的方法
- vue使用ElementUI時導(dǎo)航欄默認(rèn)展開功能的實(shí)現(xiàn)
- vue實(shí)現(xiàn)側(cè)邊欄導(dǎo)航效果
- Vue實(shí)現(xiàn)導(dǎo)航欄菜單
- vue2.0實(shí)現(xiàn)導(dǎo)航菜單切換效果
- vue+elementUI動態(tài)生成面包屑導(dǎo)航教程
- 詳解Vue的鉤子函數(shù)(路由導(dǎo)航守衛(wèi)、keep-alive、生命周期鉤子)
相關(guān)文章
vue項(xiàng)目打包解決靜態(tài)資源無法加載和路由加載無效(404)問題
這篇文章主要介紹了vue項(xiàng)目打包,解決靜態(tài)資源無法加載和路由加載無效(404)問題,靜態(tài)資源無法使用,那就說明項(xiàng)目打包后,圖片和其他靜態(tài)資源文件相對路徑不對,本文給大家介紹的非常詳細(xì),需要的朋友跟隨小編一起看看吧2023-10-10
vue3中使用highlight.js實(shí)現(xiàn)代碼高亮顯示的代碼示例
代碼高亮是在網(wǎng)頁開發(fā)中常見的需求之一,它可以使代碼在頁面上以不同的顏色或樣式進(jìn)行突出顯示提高可讀性,這篇文章主要介紹了vue3中使用highlight.js實(shí)現(xiàn)代碼高亮顯示的相關(guān)資料,需要的朋友可以參考下2025-04-04
Node.js使用orm2進(jìn)行update操作時關(guān)聯(lián)字段無法修改的解決方法
這篇文章主要給大家介紹了Node.js使用orm2進(jìn)行update操作時關(guān)聯(lián)字段無法修改的解決方法,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-06-06
vue項(xiàng)目實(shí)現(xiàn)按鈕可隨意移動
這篇文章主要為大家詳細(xì)介紹了vue項(xiàng)目實(shí)現(xiàn)按鈕可隨意移動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
公共Hooks封裝useTableData表格數(shù)據(jù)實(shí)例
這篇文章主要為大家介紹了公共Hooks封裝useTableData表格數(shù)據(jù)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12

