首页 > 代码库 > [Vue] Preload Data using Promises with Vue.js and Nuxt.js
[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=http://www.mamicode.com/"~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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。