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