“写操作失败:计算属性”帖子”被读取”

发布于 2025-02-01 03:48:10 字数 555 浏览 0 评论 0原文

我尝试从PostList中删除帖子,但是获得错误

<PostList
    :posts="sortedAndSearchedPosts"
    @remove="removePost"
    v-if="!isPostsLoading"
  />

删除邮政功能

 removePost(post) {
      this.posts = this.posts.filter((p) => p.id !== post.id);
    },

帖子在此处初始化

 computed: {
    ...mapState({
      posts: (state) => state.post.posts,
    }),
    ...mapGetters({
      sortedPosts: "post/sortedPosts",
      sortedAndSearchedPosts: "post/sortedAndSearchedPosts",
    }),
  },

I try remove post from PostList, but getting an error

<PostList
    :posts="sortedAndSearchedPosts"
    @remove="removePost"
    v-if="!isPostsLoading"
  />

remove post function

 removePost(post) {
      this.posts = this.posts.filter((p) => p.id !== post.id);
    },

posts initialized here

 computed: {
    ...mapState({
      posts: (state) => state.post.posts,
    }),
    ...mapGetters({
      sortedPosts: "post/sortedPosts",
      sortedAndSearchedPosts: "post/sortedAndSearchedPosts",
    }),
  },

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈独秀 2025-02-08 03:48:11

实际上,尚不清楚this.posts实际上是在哪里设置的。
如果我猜对您的体系结构正确,我会看到2个选项:

  • 要么直接删除商店中的帖子,因此sortedandSearchedPosts
  • 如果您不想更新全局状态, 请直接反映更改本地副本初始化帖子
data () {
   return {
      posts: [...this.sortedAndSearchedPosts],
      ...
   }
},

并更新代码以使用本地帖子列表:

<PostList
    :posts="posts"
    ...
  />

希望这会有所帮助!

Not clear where this.posts are actually initially set up.
If I guess your architecture right, I see 2 options for you:

  • either directly remove the post in your store so that sortedAndSearchedPosts reflects the change directly
  • if you don't want to update the global state, build a local copy when you initialise posts:
data () {
   return {
      posts: [...this.sortedAndSearchedPosts],
      ...
   }
},

and update your code to use the local list of posts:

<PostList
    :posts="posts"
    ...
  />

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文