Node.js/nuxt/asyncData:修订间差异
来自康健生活
| 第3行: | 第3行: | ||
== data == | == data == | ||
在 asyncData 中获取数据,return 的数据,nuxt 会自动混入到 data 中。 | 在 asyncData 中获取数据,return 的数据,nuxt 会自动混入到 data 中。 | ||
=== 拿到数据 === | |||
<syntaxhighlight lang="javascript"> | |||
async asyncData ({ params, route, error }) { | |||
const [c] = await Promise.all([ | |||
axios.get('https://api.***') | |||
]) | |||
return { | |||
bar: foo | |||
} | |||
},</syntaxhighlight> | |||
<syntaxhighlight lang="javascript"> | |||
async asyncData() { | |||
let { data } = await axios.get('https://***') | |||
return { foo: data } | |||
}</syntaxhighlight> | |||
<syntaxhighlight lang="javascript"> | |||
asyncData() { | |||
return axios.get('https://api***') | |||
.then((res) => { | |||
return {info: res.data} | |||
}) | |||
}</syntaxhighlight> | |||
== this == | == this == | ||
在页面组件中 this 的值为 undefined | 在页面组件中 this 的值为 undefined | ||