models/Index.js
import HTTP from '@/utils/http';
export default class IndexModel extends HTTP {
getHomeData () {
return new Promise((resolve, reject) => {
this.axiosGet({
url: 'api/get_home_data',
sucess (data){
resolve(data);
},
error (err) {
reject(err);
}
})
});
}
}
index.vue
使用 aysncData 方法进行异步获取数据,这是 Nuxt.js 的机制标准的方法
<script>
import IndexModel from '@/models/Index';
const indexModel = new IndexModel();
export default {
data(){
return {
sliderData: []
}
},
aync asyncData() {
const { sliderData } = await indexModel.getHomeData();
return {
sliderData
}
},
}
</script>