vue.js - 如何将数据从 Data 函数传递到 Nuxt 中的 AsyncData
问题描述
我想将数据从 Data 函数发送到 Nuxt 中的 asyncData。
我试过这个
data () {
return {
prevpage: null,
nextpage: null,
currentPage: 2,
pageNumbers: [],
pageNumberCount: 0
}
},
asyncData ({ $axios, error, data }) {
return $axios
.get('https://api.themoviedb.org/3/movie/popular?api_key=' + process.env.API_SECRET + '&page=' + this.currentPage)
.then((response) => {
return {
popularmovies: response.data.results
}
})
.catch((e) => {
error({
statusCode: 503,
message: 'Unable to fetch event'
})
})
我也试过这个
.get('https://api.themoviedb.org/3/movie/popular?api_key=' + process.env.API_SECRET, {
params: {
page: this.currentPage
}
我也尝试使用 ES6
.get(`https://api.themoviedb.org/3/movie/popular?api_key=${process.env.API_SECRET}&page=${this.currentPage}`)
但它总是返回错误
无法读取未定义的属性“currentPage”
解决方案
您可以使用 VueX 商店
asyncData ({ $axios, error, data , store}) {
// access to data
console.log(store.state.myData)
})
在您安装的集合数据到商店
mounted(){
this.$store.setData(this.pageNumberCount)
}
推荐阅读
- r - 在R中选择多个条件的变量
- concurrency - 为什么需要两个双向通道来实现单元的服务器协议?
- python - django.template.exceptions.TemplateDoesNotExist: nouveaucontact.html
- jquery - 改变至
- c++ - 错误:表达式必须是可修改的 Ivalue
- android - Gson.toJson() 在传递 Intent 时抛出 StackOverflowError
- swift - 如何将 SwiftUI 连接到 macOS 基于文档的应用程序中的文档?
- regex - 不区分大小写的单词边界
- r - R中的多次收集以创建整洁的数据集
- .net-core - 从 VS 2019 CE 到 Raspberry PI 的 .net core 3.1 远程调试 - 当应用程序想要处理 GPIO 中断时,vsdbg 退出