Home> Web Front-end> Vue.js> body text

How to implement Zhihu-like answer and comment function in Vue?

PHPz
Release: 2023-06-25 16:19:22
Original
1452 people have browsed it

Vue is a very popular front-end framework. Its flexibility and ease of use make it widely used in web development. Zhihu is a very popular Q&A community with a large user base and rich content. In Zhihu, the comment function under answers is very important. In this article, we will explore how to use Vue to implement Zhihu-like answer and comment functions.

1. Function introduction

In Zhihu, users can comment under answers. Comments can form a tree structure, where each node represents a comment and there is a parent-child relationship between nodes. Users can add their own comments under each node, which will also form a tree structure. In order to facilitate users to view, Zhihu will also expand and shrink comments.

The following is a list of functions that need to be implemented in this article:

  1. Users can add comments under answers;
  2. Comments can form a tree structure;
  3. Users can add comments under each node;
  4. Display comments according to hierarchy;
  5. Users can expand and contract comments.

2. Data structure design

Before implementing this function, we need to design the data structure first. In Zhihu, comments are in a tree structure, and each node has the following attributes:

  1. id: the unique identifier of the node;
  2. content: the content of the node;
  3. author: the author of the node;
  4. createTime: the node creation time;
  5. children: the list of child nodes.

The following is the definition of the data structure:

interface Comment { id: string; content: string; author: string; createTime: number; children?: Comment[]; }
Copy after login

For each answer, we need a list of comments. Because there may be multiple answers, we need to put these comment lists into an object, so that the corresponding comment list can be obtained by the id of the answer. The following is the definition of the data structure:

interface CommentData { [answerId: string]: Comment[]; }
Copy after login

3. Implementation steps

3.1 Display comments

We need to display the comment list first. To display a tree structure, we can use recursive components. Recursive components mean that the component can call itself in its own template.

In Vue, you can use the name attribute of the component to implement recursion. Here is a simple component:

 
Copy after login

This component will recursively render all child nodes.

3.2 Add a comment

When adding a comment, we need to specify which node to add it to. Therefore, we need to add a unique identifier to each node. In this example, we use UUID to generate a unique identifier. At the same time, for the convenience of operation, we also save the ID of the answer to which it belongs in each node.

We can use Vuex to manage state. The process of adding comments is as follows:

  1. The user enters the comment content and selects which node to add it to;
  2. Trigger Vuex's Action, send a request to the server, and add comments to the server Record;
  3. After the addition is successful, the server returns a new comment record;
  4. Trigger Vuex's Mutation to add the new comment record to the state.

In the component, we can use the v-model directive to bind the comment content entered by the user, and use the