Nuxt.js: Feature Request asyncProps

Created on 21 Nov 2019  路  2Comments  路  Source: nuxt/nuxt.js

What problem does this feature solve?

Inside setup function of the composition api in Vue 3, we cannot access this.data. It means we cannot use asyncData in composition api. So we need asyncProps api for nuxt page component which merges returning object in props. Only then we can create ref or reactive value with ssr.

What does the proposed changes look like?

import { createComponent, reactive } from '@vue/composition-api'

export default createComponent({
  props: {
    initialUsers: {
      type: Array,
      required: true
    }
  },
  async asyncProps(ctx) {
    const initialUsers = await ctx.$axios.get('/users');

    return { initialUsers };
  },
  setup (props, ctx) {
    const userData = reactive({
      users: [...props.initialUsers],
      page: 1,
    })

    function onClickMore() {
        ctx.root.$axios.get('/users', { params: { page: userData.page+1 }, }).then(res => {
          userData.users = userData.users.concat(res); 
          userData.page = userData.page +1;
        })
    }

    return {
      userData,
      onClickMore,
    }
  }
});
3.x feature-request

All 2 comments

How does it going ?

Now we can solve this issue via fetch hook.
https://composition-api.now.sh/helpers/useFetch.html

Big thanks to @danielroe 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danieloprado picture danieloprado  路  3Comments

maicong picture maicong  路  3Comments

lazycrazy picture lazycrazy  路  3Comments

o-alexandrov picture o-alexandrov  路  3Comments

shyamchandranmec picture shyamchandranmec  路  3Comments