Node.js/nuxt/asyncData:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
第2行: 第2行:
asyncData 参数只在页面组件中有效。
asyncData 参数只在页面组件中有效。


=== ctx ===
== ctx ==
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
asyncData (ctx) {
asyncData (ctx) {

2020年10月11日 (日) 16:01的版本

引用

asyncData 参数只在页面组件中有效。

ctx

asyncData (ctx) {
  // todo
},
  • $axios: (...)
  • $config: (...)
  • $cookies: (...)
  • $errorHandler: (...)
  • app: (...)
  • base: (...)
  • env: (...)
  • error: (...)
  • from: (...)
  • isDev: (...)
  • isHMR: (...)
  • isStatic: (...)
  • next: (...)
  • nuxtState: (...)
  • params: (...)
  • payload: (...)
  • query: (...)
  • redirect: (...)
  • route: (...)
  • store: (...)
  • _errored: (...)
  • _redirected: (...)
  • $axios: ƒ wrap()
  • $config: {}
  • $cookies: {parseJSON: true, set: ƒ, setAll: ƒ, get: ƒ, getAll: ƒ, …}
  • $errorHandler: ƒ (err)
  • app: {head: {…}, store: Store, router: VueRouter, nuxt: {…}, render: ƒ, …}
  • base: "/"
  • env: {host: {…}, target: "development", SECRET: "kangkk"}
  • error: ƒ ()
  • from: {name: "/down/detail", meta: Array(1), path: "/down/detail/22.html", hash: "#93", query: {…}, …}
  • isDev: true
  • isHMR: false
  • isStatic: false
  • next: ƒ ()
  • nuxtState: {layout: "default", data: Array(1), fetch: Array(0), error: null, state: {…}, …}
  • params: {id: "22", bid: 0, title: "嘻嘻嘻", image: "http://static.askgo.cn/resource/intro/img/093fb01727e34dbaa230882edfbf4f1e.png"}
  • payload: undefined
  • query: {}
  • redirect: ƒ (status, path, query)
  • route: {name: "/pay/statement", meta: Array(1), path: "/pay/statement", hash: "", query: {…}, …}
  • store: Store {_committing: false, _actions: {…}, _actionSubscribers: Array(1), _mutations: {…}, _wrappedGetters: {…}, …}
  • _errored: true
  • _redirected: false
  • __proto__: Object

详细

---> asyncdata详细内容

data

在 asyncData 中获取数据,return 的数据,nuxt 会自动混入到 data 中。

拿到数据

async asyncData ({ params, route, error }) {
  const [bar] = await Promise.all([
    axios.get('https://api.***')
  ])
  return {
    foo: bar
  }
},
async asyncData() {
  let { data } = await axios.get('https://***')
  return { foo: data }
}
asyncData() {
  return axios.get('https://api***')
  .then((res) => {
    return {foo: res.data}
  })
}
Warning: 这个时候在 data 中一定不要定义如上例中的 foo 参数,不然会拿不到数据
// 错误行为
data () {
  return {
    foo: [],
  }
},

this

在页面组件中 this 的值为 undefined