fetch 方法

fetch 方法用于在渲染页面前填充应用的状态树(store)数据, 与 asyncData 方法类似,不同的是它不会设置组件的数据。

  • 类型: Function

如果页面组件设置了 fetch 方法,它会在组件每次加载前被调用(在服务端或切换至目标路由之前)。

fetch 方法的第一个参数是页面组件的上下文对象 context,我们可以用 fetch 方法来获取数据填充应用的状态树。为了让获取过程可以异步,你需要返回一个 Promise,Nuxt.js 会等这个 promise 完成后再渲染组件。

例如 pages/index.vue

  1. <template>
  2. <h1>Stars: {{ $store.state.stars }}</h1>
  3. </template>
  4. <script>
  5. export default {
  6. fetch ({ store, params }) {
  7. return axios.get('http://my-api/stars')
  8. .then((res) => {
  9. store.commit('setStars', res.data)
  10. })
  11. }
  12. }
  13. </script>

你也可以使用 asyncawait 的模式简化代码如下:

  1. <template>
  2. <h1>Stars: {{ $store.state.stars }}</h1>
  3. </template>
  4. <script>
  5. export default {
  6. async fetch ({ store, params }) {
  7. let { data } = await axios.get('http://my-api/stars')
  8. store.commit('setStars', data)
  9. }
  10. }
  11. </script>