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

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Vue] Preload Data using Promises with Vue.js and Nuxt.js相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

从无到有构建vue实战项目

vue-cli项目中引入图片懒加载

用apicloud+vue的VueLazyload实现缓存图片懒加载

[Nuxt] Use Vuex Actions to Delete Data from APIs in Nuxt and Vue.js

vue的踩坑路

Vuejs Preload Plugin 因 HtmlWebpackPlugin.getHooks is not a function 而失败