Kos*_*Mos 2 vue.js vuejs2 nuxt.js
我有一个与 asyncData() 完美配合的组件,但我想使用 fetch() 代替。问题是,当使用 fetch 方法时,“car”属性永远不会更新。我错过了什么吗?
不起作用:
data() {
return {
car: null,
}
},
async fetch({ $axios, route }) {
this.car = await $axios.$get('/cars/' + route.params.id)
},
Run Code Online (Sandbox Code Playgroud)
完美运行:
data() {
return {
car: null,
}
},
async asyncData({ $axios, route }) {
const response = await $axios.$get('/cars/' + route.params.id)
return { car: response.data }
},
Run Code Online (Sandbox Code Playgroud)
如果您将fetch钩子与类似的东西一起使用fetch({ ... }),那么您将使用旧的fetch()钩子。
如果你想让它发挥作用,请尝试一下
async fetch() {
this.car = await this.$axios.$get(`/cars/${this.$route.params.id}`)
},
Run Code Online (Sandbox Code Playgroud)
正如本github 问题中所证实的那样。
| 归档时间: |
|
| 查看次数: |
1429 次 |
| 最近记录: |