码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2018-09-28 16:36:47      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:lock   lis   text   export   axios.get   spl   over   relative   desc   

父组件
<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;
		width: 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>

  

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

标签:lock   lis   text   export   axios.get   spl   over   relative   desc   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!