一、首先导入用到的组件和axios
五分彩系统搭建制作q<115.28.8.00.9.9>
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中将要用到的数据给一个初始值,为空
data () {
return {
swiperList: [],
iconList: [],
recommendList: [],
weekendList: []
}
},
三、在methods中写方法,发送axios获取数据
methods: {
getHomeInfo () {
axios.get(‘/api/index.json‘)
.then(this.getHomeInfoSucc)
},
getHomeInfoSucc (res) {
res=res.data
if (res.ret && res.data) {
const data = res.data
this.swiperList = data.swiperList
this.iconList = data.iconList
this.recommendList = data.recommendList
this.weekendList = data.weekendList
}
}
},
mounted () {
this.getHomeInfo()
}
}
四、传递数据
<div>
<home-header></home-header>
<home-swiper :list="swiperList"></home-swiper>
<home-icons :list="iconList"></home-icons>
<home-recommend :list="recommendList"></home-recommend>
<home-weekend :list="weekendList"></home-weekend>
</div>
五、封装 axios
通过简单的配置就可以本地调试线上环境, 这里结合业务封装了axios 。
原文地址:https://www.cnblogs.com/yihoudeyihou/p/11294284.html