vue3课程–MySpace
发帖功能的逻辑梳理
1.点击发帖会触发post_a_post这个函数;
<button @click="post_a_post" type="button" class="btn btn-primary btn-sm">发帖</button>
2.该函数会触发父组件中的post_a_post事件,该事件的参数也就是content.value发帖的内容
const post_a_post = () =>{
context.emit('post_a_post',content.value);
content.value = "";
};
3.父组件触发之后会调用post_a_post函数
<UserProfileWrite @post_a_post="post_a_post"/>
const post_a_post = (content) => {
posts.count ++;
posts.posts.unshift({
id: posts.count,
userId: 1,
content: content,
})
};
这个函数会在posts中添加一个新的帖子,新帖子的内容为(content),新帖子的类型是relative,所以当新帖子发生变化时,引用新帖子的组件就会重新渲染。