vue 的组件开发,以及swiper,axios的使用

父组件
<template> <div> <home-header :city="city"></home-header> //给子组件传递值 <home-swiper :list='swiperList'></home-swiper> <home-icons :icons="iconList"></home-icons> <home-recommend :list="recommendList"></home-recommend> <home-weekend :list="weekendList"></home-weekend> </div> </template> <script> import HomeHeader from './components/Header' import HomeSwiper from './components/Swiper' import HomeIcons from './components/Icons' import HomeRecommend from './components/Recommend' import HomeWeekend from './components/Weekend' import axios from 'axios' export default { name: 'Home', components: { HomeHeader, HomeSwiper, HomeIcons, HomeRecommend, HomeWeekend }, data (){ return { city:'', swiperList:[], iconList:[], recommendList:[], weekendList:[] } }, methods:{ getHomeInfo(){ axios.get('/api/index.json') .then(this.getHomeInfoSucc) }, getHomeInfoSucc(res){ res=res.data; if(res.res && res.data){ let data=res.data; this.city =data.city; this.swiperList = data.swiperList; this.iconList = data.iconList; this.recommendList = data.recommendList; this.weekendList = data.weekendList; } } }, mounted(){ this.getHomeInfo(); } } </script> <style> </style>

  子组件

<template>
	<div class="icons">
		<swiper :options="swiperOption">//使用swiper插件
			<swiper-slide  v-for="(page,index) of pages" :key="index">
				<div class="icon" v-for="item of page"  :key="item.id">
					<div class="iconImg" :key="item.id"   >
						<img :src="item.imgUrl" class="icon-img-content">
					</div>
					<p class="icon-desc">{{item.desc}}</p>
				</div>
				
			</swiper-slide>
		</swiper>
	</div>
</template>

<script>
	export default {
		name: 'HomeIcons',
		props:{          //获取父组件传递的值
			icons:Array
		},
		data (){
			return {
				swiperOption:{
					autoplay:false
				}
			}
		},
		 computed: {
		    pages () {
		      const pages = []
		      this.icons.forEach((item, index) => {
		        const page = Math.floor(index / 8)
		        if (!pages[page]) {
		          pages[page] = []
		        }
		        pages[page].push(item)
		      })
		      return pages
		    }
		  }
	}
</script>

<style lang="stylus" scoped>
@import '~styles/varibles.styl' 
	.icons{
		height: 0;
		padding-bottom: 50%;
		overflow: hidden;
		
	}
	.icon{
		overflow: hidden;
		height: 0;
		 25%;
		float: left;
		padding-bottom: 25%;
		position: relative;
	}
	.iconImg{
		position: absolute;
		top: 0;left: 0;right: 0;bottom: .44rem;
		box-sizing: border-box;
		padding:.05rem;
	}
	.icon-img-content{
		display: block;
		margin:0 auto;
		height: 100%;
	}
	.icon-desc{
		position: absolute;
		left: 0;right: 0;bottom: 0;
		height: .44rem;
		line-height: .44rem;
		color:$ftColor;
		text-align: center;
	}
</style>

  

原文地址:https://www.cnblogs.com/chenlw/p/9718887.html