vue中使用swiper做轮播页面,标题样式随着轮播页面改变而改变

vue中使用swiper做轮播页面,标题样式随着轮播页面改变而改变

知识点: 标题组件与轮播组件之间交互(vue兄弟组件之间传值)

兄弟之间传递数据需要借助于事件车,通过事件车的方式传递数据

1、创建一个Vue的实例,让各个兄弟共用同一个事件机制。

2、传递数据方,通过一个事件触发bus.$emit(方法名,传递的数据)。

3、接收数据方,通过mounted(){}触发bus.$on(方法名,function(接收数据的参数){用该组件的数据接收传递过来的数据}),此时函数中的this已经发生了改变,可以使用箭头函数。

实例如下:

我们可以创建一个单独的js文件eventVue.js,内容如下

import Vue from 'vue';
 
export default new Vue();

  

整个文件这两句就够啦!!!!

标题组件

子组件1

  • 文件名: nav.vue
 1 <template>
 2   <div class="nav-container">
 3     <ul style="padding: 0">
 4       <li v-for="(item, index) in newBreadcrumb" 
 5       :key="index"
 6       :class="nowIndex===index ? 'selected-nav nav-title':'unselected-nav nav-title'">
 7       {{item.name}}
 8       <span class="line">/</span>
 9       </li>
10     </ul>
11   </div>
12 </template>
13 <script>
14 import eventHub from '../../../../commonJS/eventVue.js'
15 export default {
16   props:['breadcrumb'],
17   data(){
18     return {
19       newBreadcrumb: [],
20       nowIndex: 0,
21     }
22   },
23   created(){
24     console.log("breadcrumb", this.breadcrumb) //父子组件传值,传过来一个标题数组
25     //对数组进行处理,转为数组对象,给原数组成员加了key,为name
26     for(let i in this.breadcrumb){
27       let breadcrumbObject = {}
28       this.$set(breadcrumbObject, 'name', this.breadcrumb[i] )
29       this.newBreadcrumb.push(breadcrumbObject)
30     }
31     
32   },
33   mounted(){
34     //nav组件接收swiper组件滑动时传过来的index
35     eventHub.$on('slideTab', (index)=>{
36       this.nowIndex = index
37     });
38   },
39 }
40 </script>
41 <style>
42 .nav-container {
43   font-size: 0;
44   padding: .2rem 0 0 .2rem;
45 }
46 .nav-title {
47   font-family: "DINPro-Medium";
48   font-size: 0.18rem;
49 }
50 li {
51   display:inline-block;
52 }
53 .nav-title:nth-child(3) .line{
54   display: none;
55 }
56 .selected-nav {
57   color: #554344;
58 }
59 .unselected-nav {
60   color: #B8B8B8;
61 }
62 </style>
子组件2
  • 文件名: swiper.vue
 1 <template>
 2   <div router>
 3     <div v-for="(route, index) in $router.options.routes" :key="index">
 4           <!-- {{route.name}} -->
 5       <div v-if="route.name === 'CompetitionObjective'">
 6         <template v-for="(items, index) in route.children">
 7             <swiper
 8             :key="index"
 9             v-if="items.path === 'MRtableAssociation/'"
10             :options="swiperOption"
11             ref="mySwiper"
12             >
13             <swiper-slide
14                 v-for="(item, index) in items.children"
15                 :key="index"
16                 class="mdrt-item"
17             >
18                 <keep-alive>
19                 <component :is="item.component" v-bind:MDRTcompetitionCd="MDRTcompetitionCd" v-if="MDRTcompetitionCd.length>0"></component>
20                 </keep-alive>
21             </swiper-slide>
22             <div class="swiper-pagination" slot="pagination"></div>
23             </swiper>
24         </template>
25       </div>
26     </div>
27   </div>
28 </template>
29 <script>
30 import eventHub from '../../../../commonJS/eventVue.js'
31 export default {
32   props:{
33     MDRTcompetitionCd: {
34       type: String,
35       default: ""
36     }
37   },
38   data(){
39     return {
40       swiperOption: {
41         pagination: {
42           el: ".swiper-pagination"
43         },
44       }
45     }
46   },
47   mounted() {
48       var mySwiper = this.$refs['mySwiper'][0].swiper
49       mySwiper.on('slideChange', () => { // 监控滑动后当前页面的索引,将索引发射到导航组件
50       // 左右滑动时将当前slide的索引发送到nav组件,eventHub是兄弟组件之间传值的桥梁
51       eventHub.$emit('slideTab', mySwiper.activeIndex);
52     });
53   }
54 }
55 </script>
56 <style scoped>
57 .swiper-table {
58   height: 3.65rem;
59   border-top: 0.01rem solid #d8d8d8;
60 }
61 
62 .swiper-table >>> .swiper-container-horizontal > .swiper-pagination-bullets {
63   bottom: -0.2rem;
64   left: 0;
65    100%;
66   z-index: 1000;
67 }
68 .swiper-table >>> .swiper-pagination-bullet-active {
69   background: #d31145 !important;
70   /*  .34rem !important; */
71 }
72 .mdrt-item {
73   font-family: 'DINPro-Medium';
74   font-size: .18rem;
75   color: #554344 !important;
76 } 
77 </style>

父组件:

关键代码

<v-nav :breadcrumb='breadcrumb' v-if="breadcrumb.length > 0"></v-nav>
<v-swiper :MDRTcompetitionCd='MDRTcompetitionCd'></v-swiper>

  




如有问题欢迎指正:qq: 1534147975

原文地址:https://www.cnblogs.com/sinceForever/p/12146091.html