如何在NuxtJS中从json文件中获取单个帖子id数据?
created() {
this.getProductsData()
},
methods: {
getProductsData() {
const vm = this
this.$axios
.get(`/json/products.json`)
.then(function(response) {
vm.item = response.data.products
})
.catch((error) => console.log(error))
}
}
最佳答案
要从特定 ID 获取帖子,您可以根据其 _id
过滤数据,然后获取返回的第一个条目(因为应该只有一个具有唯一的 :
methods: {
getProductsData() {
const vm = this
this.$axios
.get(`/json/products.json`)
.then(function(response) {
vm.item = response.data.products.filter((item) => {
return item._id === 'spekkoek-panden';
})[0]
})
.catch((error) => console.log(error))
}
}
关于javascript - 从 NuxtJS 中的 json 文件获取单个帖子 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59382699/