asyncData只能在pages目录下的页面组件中使用,而不能在components中的组件中使用
组件中获取数据
created() {
this.getComments()
},
methods: {
async getComments() {
console.log(this.$route.params)
let res = await getComment(this.$route.params.id, this.cur, this.limit)
console.log(res)
}
}
页面组件中获取数据(当然之前的方法也可以用)
// 这种方式用在页面中能将返回的数据与data进行混合
export default {
name: 'IndexPage',
data () {
return {
}
},
async asyncData() {
let res = await getIndexContent()
console.log(res)
return res.data.items
}
}
// items = {
//delicacyList: (5) [{…}, {…}, {…}, {…}, {…}, __ob__: Observer]
//hotelList: (5) [{…}, {…}, {…}, {…}, {…}, __ob__: Observer]
//scenicSpotList: (5) [{…}, {…}, {…}, {…}, {…}, __ob__: Observer]
//}
// 获取数据之后的data
data () {
return {
delicacyList: [{…}, {…}, {…}, {…}, {…}]
hotelList: [{…}, {…}, {…}, {…}, {…}]
scenicSpotList: [{…}, {…}, {…}, {…}, {…}]
}
},
Q.E.D.