Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Describe how to add asyncData type in TypeScript #174

Open
ktsn opened this issue Apr 4, 2018 · 3 comments
Open

Describe how to add asyncData type in TypeScript #174

ktsn opened this issue Apr 4, 2018 · 3 comments

Comments

@ktsn
Copy link
Member

ktsn commented Apr 4, 2018

ref vuejs/vuex#1214

It maybe good if we have a docs for how to declare asyncData in TypeScript. It is not an API of any libraries, so we cannot directly declare it in any d.ts files. I guess it would be appropriate to describe it in the SSR docs.

@yyx990803
Copy link
Member

Yeah, feel free to submit a PR

@TonyPythoneer
Copy link

TonyPythoneer commented Jul 6, 2018

Maybe like this?

import Vue, { ComponentOptions } from "vue";
import { Store } from "vuex";
import { Route } from 'vue-router';

declare module "vue/types/options" {

    interface ComponentOptions<V extends Vue> {
        asyncData?: (store: Store<any>, route: Route) => Promise<any>;
    }
}


declare module "vue/types/vue" {
    interface Vue {
        asyncData?: (store: Store<any>, route: Route) => Promise<any>;
    }
}

But I am confused about this, it doesn't have a standard method or formal document mentions it.

@joffreyBerrier
Copy link

joffreyBerrier commented Jun 5, 2019

Do like this:

  @Component({
    async asyncData({ params, error }) {
     💀// impossible to use this.$http on @component 💀
      const http = Vue.prototype.$nuxt.$http;
      const payload1 = await http.get('/url')
      const payload2 = await http.post('/url', { params })

      return {
        data1: payload1.data,
        data2: payload2.data,
      }
    }
  })
  export default class NameOfYourClass extends Vue {
    name: 'NameOfYourClass'

    // AsyncData
    data1?: type
    data2?: type

    [...]
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants