【问题标题】:Get single post id from json file in NuxtJS从 NuxtJS 中的 json 文件获取单个帖子 ID
【发布时间】:2019-12-17 21:58:24
【问题描述】:

如何在 NuxtJS 中从 json 文件中获取单个 post 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?
  • 我想获得单个帖子,因为我已经拥有所有数据
  • let postId = response.data.products[0]._id 呢?
  • 我现在正在获取第一个 json,但是我怎样才能通过正确的 URL 获得正确的 id?
  • 我认为我们在这里遗漏了一些信息。您能粘贴更多代码并详细说明问题吗?

标签: javascript arrays json vue.js vuejs2


【解决方案1】:

要从特定 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))
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-20
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    相关资源
    最近更新 更多