具體代碼如下所示:
專注于為中小企業(yè)提供網(wǎng)站設計制作、成都網(wǎng)站建設服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)汪清免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了成百上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉(zhuǎ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>評論內(nèi)容:</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ù)的綁定,記住組件內(nèi)的數(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()方法來調(diào)用父組件的方法 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>
網(wǎng)頁名稱:vue實現(xiàn)評論列表功能-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://aaarwkj.com/article20/dihgjo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、品牌網(wǎng)站制作、網(wǎng)站維護、微信公眾號、面包屑導航、網(wǎng)站設計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容