vue實(shí)現(xiàn)評(píng)論列表功能
更新時(shí)間:2019年10月25日 11:09:51 作者:angle-xiu
本文通過實(shí)例代碼給大家介紹了vue實(shí)現(xiàn)評(píng)論列表功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
具體代碼如下所示:
<!DOCTYPE html> <html> <head> <title>簡(jiǎn)易評(píng)論列表</title> <meta charset="utf-8"> <link rel="stylesheet" href="node_modules\bootstrap\dist\css\bootstrap.css" rel="external nofollow" rel="external nofollow" > </head> <body> <div id="app"> <ul class="list-group"> <!-- 為事件綁定別稱時(shí)不要使用駝峰命名 --> <box @plocalcoments="localComents"></box> <li class="list-group-item" v-for="item in list" :key="item.id"> <span class="badge">評(píng)論人: {{item.user}}</span> {{item.content}} </li> </ul> </div> <template id="temp"> <div> <div class="form-group"> <label>評(píng)論人:</label> <input type="text" class="form-control" v-model="user"> </div> <div class="form-group"> <label>評(píng)論內(nèi)容:</label> <textarea class="form-control" v-model="content"></textarea> </div> <div class="form-group"> <input type="button" value="發(fā)表評(píng)論" class="btn btn-primary" @click="add"> </div> </div> </template> </body> <script src="node_modules\vue\dist\vue.js"></script> <script> let commentBox = {//定義評(píng)論組件 data(){//進(jìn)行數(shù)據(jù)的綁定,記住組件內(nèi)的數(shù)據(jù)是一個(gè)方法 return{ user:'', content:'' } }, template:"#temp", methods:{ add(){//評(píng)論添加函數(shù) //獲取當(dāng)前評(píng)論 let comment = {id:Date.now(),user:this.user,content:this.content}; //從localStorage讀取列表 let list = JSON.parse(localStorage.getItem('cmts')|| '[]');//若不存在cmts則返回空數(shù)組,避免json解析出錯(cuò) if(comment.user&&comment.content)//進(jìn)行判空 list.unshift(comment); localStorage.setItem('cmts',JSON.stringify(list)); this.user=this.content='';//清空評(píng)論列表 //利用$emit()方法來調(diào)用父組件的方法 this.$emit('plocalcoments'); } } } let vm = new Vue({ el:"#app", data:{ list:[] }, components:{ box:commentBox }, created(){ //實(shí)例創(chuàng)建后加載評(píng)論 this.localComents(); }, methods:{ localComents(){ let list = localStorage.getItem('cmts'||'[]');//若不存在cmts則返回空數(shù)組,避免json解析出錯(cuò) this.list = JSON.parse(list);//刷新數(shù)據(jù) } } }); </script> </html>
<!DOCTYPE html> <html> <head> <title>簡(jiǎn)易評(píng)論列表</title> <meta charset="utf-8"> <link rel="stylesheet" href="node_modules\bootstrap\dist\css\bootstrap.css" rel="external nofollow" rel="external nofollow" > </head> <body> <div id="app"> <ul class="list-group"> <!-- 為事件綁定別稱時(shí)不要使用駝峰命名 --> <box @plocalcoments="localComents"></box> <li class="list-group-item" v-for="item in list" :key="item.id"> <span class="badge">評(píng)論人: {{item.user}}</span> {{item.content}} </li> </ul> </div> <template id="temp"> <div> <div class="form-group"> <label>評(píng)論人:</label> <input type="text" class="form-control" v-model="user"> </div> <div class="form-group"> <label>評(píng)論內(nèi)容:</label> <textarea class="form-control" v-model="content"></textarea> </div> <div class="form-group"> <input type="button" value="發(fā)表評(píng)論" class="btn btn-primary" @click="add"> </div> </div> </template> </body> <script src="node_modules\vue\dist\vue.js"></script> <script> let commentBox = {//定義評(píng)論組件 data(){//進(jìn)行數(shù)據(jù)的綁定,記住組件內(nèi)的數(shù)據(jù)是一個(gè)方法 return{ user:'', content:'' } }, template:"#temp", methods:{ add(){//評(píng)論添加函數(shù) //獲取當(dāng)前評(píng)論 let comment = {id:Date.now(),user:this.user,content:this.content}; //從localStorage讀取列表 let list = JSON.parse(localStorage.getItem('cmts')|| '[]');//若不存在cmts則返回空數(shù)組,避免json解析出錯(cuò) if(comment.user&&comment.content)//進(jìn)行判空 list.unshift(comment); localStorage.setItem('cmts',JSON.stringify(list)); this.user=this.content='';//清空評(píng)論列表 //利用$emit()方法來調(diào)用父組件的方法 this.$emit('plocalcoments'); } } } let vm = new Vue({ el:"#app", data:{ list:[] }, components:{ box:commentBox }, created(){ //實(shí)例創(chuàng)建后加載評(píng)論 this.localComents(); }, methods:{ localComents(){ let list = localStorage.getItem('cmts'||'[]');//若不存在cmts則返回空數(shù)組,避免json解析出錯(cuò) this.list = JSON.parse(list);//刷新數(shù)據(jù) } } }); </script> </html>
效果圖:
總結(jié)
以上所述是小編給大家介紹的vue實(shí)現(xiàn)評(píng)論列表功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
您可能感興趣的文章:
- vue實(shí)現(xiàn)發(fā)表評(píng)論功能
- vue實(shí)現(xiàn)文章評(píng)論和回復(fù)列表
- VUE+Java實(shí)現(xiàn)評(píng)論回復(fù)功能
- Vue組件實(shí)現(xiàn)評(píng)論區(qū)功能
- vue開發(fā)實(shí)現(xiàn)評(píng)論列表
- vue實(shí)現(xiàn)評(píng)論列表
- Vue實(shí)現(xiàn)簡(jiǎn)單的發(fā)表評(píng)論功能
- Vuepress 搭建帶評(píng)論功能的靜態(tài)博客的實(shí)現(xiàn)
- Vue.js實(shí)現(xiàn)文章評(píng)論和回復(fù)評(píng)論功能
- vue組件實(shí)現(xiàn)發(fā)表評(píng)論功能
相關(guān)文章
詳解Vue調(diào)用手機(jī)相機(jī)和相冊(cè)以及上傳
這篇文章主要介紹了Vue調(diào)用手機(jī)相機(jī)及上傳,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05vue中pinia數(shù)據(jù)一直重復(fù)獲取之前的值的解決方法
這篇文章主要介紹了vue中pinia數(shù)據(jù)一直重復(fù)獲取之前的值的解決方法,如果想讓pinia數(shù)據(jù)不會(huì)重復(fù)獲取之前的值需要手動(dòng)強(qiáng)制觸發(fā) Pinia store 的狀態(tài)更新,文中有詳細(xì)的解決方法,需要的朋友可以參考下2024-04-04vue/cli?配置動(dòng)態(tài)代理無需重啟服務(wù)的操作方法
vue-cli是vue.js的腳手架,用于自動(dòng)生成vue.js+webpack的項(xiàng)目模板,分為vue?init?webpack-simple?項(xiàng)目名和vue?init?webpack?項(xiàng)目名兩種,這篇文章主要介紹了vue/cli?配置動(dòng)態(tài)代理,無需重啟服務(wù),需要的朋友可以參考下2022-05-05vue構(gòu)建動(dòng)態(tài)表單的方法示例
這篇文章主要介紹了vue構(gòu)建動(dòng)態(tài)表單的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09