Node.js/nuxt/asyncData:修订间差异
来自康健生活
| 第1行: | 第1行: | ||
== 引用 == | == 引用 == | ||
asyncData 参数只在页面组件中有效。 | asyncData 参数只在页面组件中有效。 | ||
=== ctx === | |||
<syntaxhighlight lang="javascript"> | |||
asyncData (ctx) { | |||
// todo | |||
},</syntaxhighlight> | |||
* $axios: (...) | |||
* $config: (...) | |||
* $cookies: (...) | |||
* $errorHandler: (...) | |||
* app: (...) | |||
* base: (...) | |||
* env: (...) | |||
* error: (...) | |||
* from: (...) | |||
* isDev: (...) | |||
* isHMR: (...) | |||
* isStatic: (...) | |||
* next: (...) | |||
* nuxtState: (...) | |||
* params: (...) | |||
* payload: (...) | |||
* query: (...) | |||
* redirect: (...) | |||
* route: (...) | |||
* store: (...) | |||
* _errored: (...) | |||
* _redirected: (...) | |||
== data == | == data == | ||
在 asyncData 中获取数据,return 的数据,nuxt 会自动混入到 data 中。 | 在 asyncData 中获取数据,return 的数据,nuxt 会自动混入到 data 中。 | ||
2020年9月22日 (二) 10:02的版本
引用
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: (...)
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}
})
}
// 错误行为
data () {
return {
foo: [],
}
},
this
在页面组件中 this 的值为 undefined
No categories