首页 > 解决方案 > 如何从 route.params 中获取 id 从 vuex 的数组中找到它并获取此 id 的标题字段

问题描述

需要帮助,如何从 route.params 中获取 id 从 vuex 的数组中找到它并获取此 id 的标题字段

在此处输入图像描述

标签: vue.jsvuexnuxt.js

解决方案


以下示例是否回答了您的问题?

// short
const title = this.$store.state.ALL_CATS.find(element => element.id == this.$route.params.id).title;
// breakdown
const id = this.$route.params.id;
const cats = this.$store.state.ALL_CATS;
const cat = cats.find(element => element.id == id);
const title = cat.title;

推荐阅读