首页 > Web开发 > 详细

[Vue] Preload Data using Promises with Vue.js and Nuxt.js

时间:2016-12-09 00:25:13      阅读:466      评论:0      收藏:0      [点我收藏+]

Nuxt.js allows you to return a Promise from your data function so that you can asynchronously resolve data before displaying the page. This allows the server to fetch the data and render the page once it‘s ready.

 

<template>
  <section class="container">
    <img src="~static/logo.png" alt="Nuxt.js Logo" />
    <ul>
      <li v-for="person in people">{{person.name}}</li>
    </ul>
  </section>
</template>

<style scoped>
.title
{
  margin: 50px 0;
}
</style>

<script>

import axios from axios
const api = `https://swapi-json-server-nvaxelgbew.now.sh/people`

  export default {
    data(){
      return axios.get(api).then((res) => ({
        people: res.data
      }))
    }
  }
</script>

 

When first loading the page, you won‘t see ui do the api call. Becuase the data is already fetched in the server. If you navigate around ‘about‘ & ‘home‘ page, u will see the api is called, becasue than it is loaded from clienet

[Vue] Preload Data using Promises with Vue.js and Nuxt.js

原文:http://www.cnblogs.com/Answer1215/p/6146827.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!