search
HomeWeb Front-endVue.jsHow to use vuejs to implement comment function

How to use vuejs to implement the comment function: 1. Use the article-content component to bind an obj; 2. Implement the comment function through the commemt-content component.

How to use vuejs to implement comment function

The operating environment of this article: windows7 system, vue2.9.6 version, DELL G3 computer.

How to use vuejs to implement the comment function?

Vue.js implements the article comment and reply comment functions:

I originally wanted to I used jade to render this page and the comment section using vue, but after thinking about it, I found it troublesome. In the end, I decided to use the entire vue component to complete it.
First go to the online demo: http://jsbin.com/ceqifo/1/edit?js,output

Then go to the renderings

You can comment directly. Click on other people's comments to reply to other people's comments.

html

<div id="comment">
 <article-content v-bind:article="article"></article-content>
 <commemt-content v-bind:comment="comment" v-on:change="changCommmer"></commemt-content>
 <comment-textarea v-bind:type="type" v-bind:name="oldComment" v-on:submit="addComment" v-on:canel="canelCommit"></comment-textarea>
</div>

The data is all bound by the root component. Several events are also monitored here.

Let’s talk about a simpler one first, , the component of the text box.

Vue.component(&#39;commentTextarea&#39;,{
 template:&#39;\
 <div class="commentBox">\
 <h3 id="发表评论">发表评论</h3>\
 <b v-if="type">你回复 {{name}}</b>\
 <textarea name="" value="请填写评论内容" v-model="commentText"></textarea>\
 <button class="btn" @click="addComment">发表</button>\
 <button class="btn" @click="canelComment">取消</button>\
 </div>&#39;,
 props: [&#39;type&#39;,&#39;name&#39;],
 data: function(){
 return {commentText:""}
 },
 methods: {
 addComment: function() {
 this.$emit("submit",this.commentText);
 this.commentText = "";
 },
 canelComment: function() {
 this.$emit("canel");
 this.commentText = "";
 }
 }
});

type means that if you click on someone else's comment, a prompt box "You reply to xxx" will be displayed. This is because there is cross-component communication and the two components are not parent-child components but are brother components. I hung up their communication. Implement communication on a property of the parent component.

The content in the text box is two-way bound with a v-model. If OK is clicked, an addComment event is triggered and the text content is passed to the parent component, which will listen for the event.

commemt-content component – ​​comment content

First determine the json format

comment: [
 {
 name: "有毒的黄同学", //评论人名字
 time: "2016-08-17", 
 content: "好,讲得非常好,good",
 reply: [ //回复评论的信息,是一个数组,如果没内容就是一个空数组
 {
 responder: "傲娇的", //评论者
 reviewers: "有毒的黄同学", //被评论者
 time: "2016-09-05",
 content: "你说得对"
 }
 }
 ]

Then look at the commemt-content component

Vue.component(&#39;commemt-content&#39;,{
 template:&#39;\
 <div class="commentBox">\
 <h3 id="评论">评论</h3>\
 <p v-if="comment.length==0">暂无评论,我来发表第一篇评论!</p>\
 <div v-else>\
 <div class="comment" v-for="(item,index) in comment" v-bind:index="index" >\
 <b>{{item.name}}<span>{{item.time}}</span></b>\
 <p @click="changeCommenter(item.name,index)">{{item.content}}</p>\
 <div v-if="item.reply.length > 0">\
  <div class="reply" v-for="reply in item.reply">\
  <b>{{reply.responder}}  回复  {{reply.reviewers}}<span>{{reply.time}}</span></b>\
  <p @click="changeCommenter(reply.responder,index)"">{{reply.content}}</p>\
  </div>\
 </div>\
 </div>\
 </div>\
 </div>&#39;,
 props: [&#39;comment&#39;],
 methods: {
 changeCommenter: function(name,index) {
 this.$emit("change",name,index);
 }
 }
});

If there is no content, it will display "No comments yet, I will post the first comment!". If there is content, start traversing. Because you need to know the number of comments when clicking on them, each comment needs to be bound to a v-bind:index="index"

When it comes to the second comment, it is still necessary to traverse the reply array and bind the binding. Because even if the content is clicked, it is added to the bottom of the first-level comment, so I bound the two click events to the same event. It's just that the names passed in are different. The index behind them is the index of first-level comments.

The changeCommenter event triggers the change, and the parent component listens and performs the corresponding behavior.

Parent component

var comment = new Vue({
 el: "#comment",
 data: {
 commenter: "session", //评论人,这里会从session拿
 type: 0, //0为评论作者1为评论别人的评论
 oldComment: null, //久评论者的名字
 chosedIndex: -1, //被选中的评论的index
 article: {
 title: "当归泡水喝的九大功效",
 time: "2016-07-12",
 read:50,
 content: ""
 },
 comment: [] //评论内容
 },
 methods: {
 //添加评论
 addComment: function(data) {
 if(this.type == 0) {
 this.comment.push({
  name: &#39;session&#39;,
  time: getTime(),
  content: data,
  reply: []
 });
 //服务器端
 }else if(this.type == 1){
 this.comment[this.chosedIndex].reply.push({
  responder: &#39;session&#39;,
  reviewers:this.comment[this.chosedIndex].name,
  time: getTime(),
  content: data
 });
 this.type = 0;
 }
 },
 //监听到了点击了别人的评论
 changCommmer: function(name,index) {
 this.oldComment = name;
 this.chosedIndex = index;
 this.type = 1;
 },
 //监听到了取消评论
 canelCommit: function() {
 this.type = 0;
 }
 }
})

data there. . . It’s really difficult to name. . . commenter is the current login name, which will be obtained from the session; type is to see whether it is the comment author or someone else's comment; oldComment is the commenter's name (it should be the id when actually stored); chosenIndex is the clicked The index of the comments.

canelCommit is to monitor the cancel comment event. This is used if I click on someone else's comment but suddenly I want to change the comment author. So set type=0;

changCommmer monitors when someone else clicks on a comment and wants to reply to the comment. That is, type=1.

addComment is to listen to the add comment event. According to the value of type, push the corresponding array. Also remember to connect with the database here. There are two ways to transfer data. Here, it is divided into two URLs or one according to the type, which depends on the design of the table. After I design the table tomorrow, I will add an http request to complete the comment function.

It’s the end of the term. I am really afraid of failing the exam.

To add:

Since it is the first time to design the table structure of the database by myself, it is very problematic.
Update, the correct table structure should be that each comment has its own id. There is a parentId attribute that defaults to null. If it is a direct comment, the parentId value is null. If it is a reply to someone else's comment, the parentId is The id of that comment. After finally finding out each piece of data, the obj is assembled based on whether there is a parentId in it and passed to the front end. If you directly group this obj, the for loop will be 3 times, so. . . I plan to use the hash in the data structure instead of using the for loop so many times. Finish this this weekend and it will be the next article.

However, after I thought about it. If you only use hashing, you cannot sort based on time. Because the hash is inserted based on the value of id%length, there is no time to sort. If you combine this obj directly based on the array returned by querying the database, the id inserted earlier must be first, so there is a time order. This data structure problem is really not simple.

Recommended: "The latest 5 vue.js video tutorial selections"

The above is the detailed content of How to use vuejs to implement comment function. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
VUE3快速入门:使用Vue.js指令实现选项卡切换VUE3快速入门:使用Vue.js指令实现选项卡切换Jun 15, 2023 pm 11:45 PM

本文旨在帮助初学者快速入手Vue.js3,实现简单的选项卡切换效果。Vue.js是一个流行的JavaScript框架,可用于构建可重用的组件、轻松管理应用程序的状态和处理用户界面的交互操作。Vue.js3是该框架的最新版本,相较于之前的版本变动较大,但基本原理并未改变。在本文中,我们将使用Vue.js指令实现选项卡切换效果,目的是让读者熟悉Vue.js的

Flask + Vue.js:快速实现单页面应用Flask + Vue.js:快速实现单页面应用Jun 17, 2023 am 09:06 AM

随着移动互联网和Web技术的迅速发展,越来越多的应用需要提供流畅、快速的用户体验。传统的多页面应用已经无法满足这些需求,而单页面应用(SPA)则成为了解决方案之一。那么,如何快速实现单页面应用呢?本文将介绍如何利用Flask和Vue.js来构建SPA。Flask是一个使用Python语言编写的轻量级Web应用框架,它的优点是灵活、易扩

VUE3基础教程:使用Vue.js插件封装图片上传组件VUE3基础教程:使用Vue.js插件封装图片上传组件Jun 15, 2023 pm 11:07 PM

VUE3基础教程:使用Vue.js插件封装图片上传组件Vue.js是一款流行的前端框架,它使开发者可以用更少的代码创建更高效、灵活的应用程序。尤其是在Vue.js3发布之后,它的优化和改进使得更多的开发者倾向于使用它。这篇文章将介绍如何使用Vue.js3来封装一个图片上传组件插件。在开始之前,需要先确保已经安装了Vue.js和VueCLI。如果尚未安装

VUE3基础教程:使用Vue.js插件封装日历组件VUE3基础教程:使用Vue.js插件封装日历组件Jun 15, 2023 pm 09:09 PM

Vue.js是现代化的前端JavaScript框架之一,它提供了一套完整的工具来构建交互式用户界面。在Vue.js的生态系统中,有各种各样的插件和组件,可以大大简化我们的开发流程。在本篇文章中,我们将介绍如何使用Vue.js插件封装一个日历组件,以方便我们在Vue.js项目中快速使用。Vue.js插件Vue.js插件可以扩展Vue.js的功能。它们可以添加全

Vue.js实现登录验证的完整指南(API、JWT、axios)Vue.js实现登录验证的完整指南(API、JWT、axios)Jun 09, 2023 pm 04:04 PM

Vue.js是一种流行的JavaScript框架,用于构建动态Web应用程序。实现用户登录验证是开发Web应用程序的必要部分之一。本文将介绍使用Vue.js、API、JWT和axios实现登录验证的完整指南。创建Vue.js应用程序首先,我们需要创建一个新的Vue.js应用程序。我们可以使用VueCLI或手动创建一个Vue.js应用程序。安装axiosax

VUE3开发入门教程:使用Vue.js组件封装chart图表VUE3开发入门教程:使用Vue.js组件封装chart图表Jun 15, 2023 pm 10:29 PM

随着大数据时代的到来,数据可视化已经成为了现如今的趋势之一。在Web前端开发的过程中,如何使用Vue.js进行数据可视化处理,成为了许多前端开发者所关注的问题。本文将会介绍如何使用Vue.js组件,封装基于chart.js库的图表。1.了解chart.jsChart.js是一款基于HTML5CanvasElement的简单易用、跨平台的开源图表库,我们可

VUE3基础教程:使用Vue.js自定义事件VUE3基础教程:使用Vue.js自定义事件Jun 15, 2023 pm 09:43 PM

Vue.js是一款流行的JavaScript框架,它提供了很多方便的特性,所以它在开发Web应用程序时非常有用。Vue.js中的自定义事件系统使其更加灵活,并且可以通过组件事件触发和处理来实现更好的代码重用性。在本文中,我们将讨论如何使用Vue.js的自定义事件。Vue.js中自定义事件的基础在Vue.js中,我们可以通过v-on指令来监听DOM事件。例如,

使用Python与Vue.js开发实时同步的Web应用程序使用Python与Vue.js开发实时同步的Web应用程序Jun 17, 2023 am 08:28 AM

随着Web应用程序的普及和用户体验的要求不断提高,实时同步已经成为了现代Web应用程序不可或缺的功能。在本文中,我们将介绍如何使用Python和Vue.js开发实时同步的Web应用程序。为了实现实时同步的功能,我们需要使用一些现代化的Web技术,其中包括WebSocket、异步编程和前端框架。以下是本文中将用到的技术栈:Python3.6+FlaskFla

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)