vue 的组件开发,以及swiper,axios的使用
Posted chenlw101
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; 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的使用的主要内容,如果未能解决你的问题,请参考以下文章