javascript - 从 NuxtJS 中的 json 文件获取单个帖子 ID
问题描述
如何从 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))
}
}