<template>
<main>
<div>
<BlogPostItem
v-for="post in posts"
:key="post.id"
v-bind="post"
@deletePost="handleDeletePost"
/>
</div>
</main>
</template>
<script>
// 引入子组件
import BlogPostItem from "./components/BlogPostItem.vue";
export default {
// 注册子组件
components: {
BlogPostItem,
},
data() {
return {
posts: [
{ id: 1, title: "Post 1", link: "https://some.url.com" },
{ id: 2, title: "Post 2", link: "https://some.url.com" },
{ id: 3, title: "Post 3", link: "https://some.url.com" },
],
};
},
methods: {
// 嗲用子组件传递过来的方法,里面的参数就是子组件传递过来的参数
handleDeletePost(id) {
this.posts = this.posts.filter((p) => p.id !== id);
},
},
};
</script>