models/Index.js

    1. import HTTP from '@/utils/http';
    2. export default class IndexModel extends HTTP {
    3. getHomeData () {
    4. return new Promise((resolve, reject) => {
    5. this.axiosGet({
    6. url: 'api/get_home_data',
    7. sucess (data){
    8. resolve(data);
    9. },
    10. error (err) {
    11. reject(err);
    12. }
    13. })
    14. });
    15. }
    16. }

    index.vue
    使用 aysncData 方法进行异步获取数据,这是 Nuxt.js 的机制标准的方法

    1. <script>
    2. import IndexModel from '@/models/Index';
    3. const indexModel = new IndexModel();
    4. export default {
    5. data(){
    6. return {
    7. sliderData: []
    8. }
    9. },
    10. aync asyncData() {
    11. const { sliderData } = await indexModel.getHomeData();
    12. return {
    13. sliderData
    14. }
    15. },
    16. }
    17. </script>