码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2016-12-09 00:28:22      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:about   const   loaded   round   out   read   ati   and   ready   

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

标签:about   const   loaded   round   out   read   ati   and   ready   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!