我正在使用 Vue.js 3,但我認(rèn)為這與我的問(wèn)題無(wú)關(guān)。我調(diào)用 localStorage 來(lái)獲取帖子對(duì)象的 JSON 數(shù)組,對(duì)其進(jìn)行解析,然后使用 id 來(lái)查找帖子。我從路線中獲取 id 沒(méi)有問(wèn)題,但使用 .find 搜索解析的數(shù)組返回未定義。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);? ? ? ??? ? }
使用 .find 方法不會(huì)從解析的 JSON 返回對(duì)象
qq_笑_17
2023-06-29 22:34:46
