我正在使用 Vue.js 3,但我认为这与我的问题无关。我调用 localStorage 来获取帖子对象的 JSON 数组,对其进行解析,然后使用 id 来查找帖子。我从路线中获取 id 没有问题,但使用 .find 搜索解析的数组返回未定义。Post.vue 文件 created() { this.postId = this.$route.params.id; console.log("****** POST ID *********"); console.log(this.postId); var posts = JSON.parse(localStorage.getItem("posts")); console.log("****** POSTS ARRAY *********"); console.log(posts); this.post = posts.find(post => post.id === this.postId); console.log("****** POST *********"); console.log(this.post); }
1 回答

12345678_0001
TA贡献1802条经验 获得超5个赞
类型this.postId
为字符串,数组中每个对象的id类型posts
为数字。所以你可以使用==
代替===
. 或者使用 将 的类型更改this.postId
为数字parseInt
。
添加回答
举报
0/150
提交
取消