vue實(shí)現(xiàn)評論列表功能

具體代碼如下所示:

成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、汾西網(wǎng)絡(luò)推廣、微信小程序開發(fā)、汾西網(wǎng)絡(luò)營銷、汾西企業(yè)策劃、汾西品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供汾西建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:bm7419.com

<!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">
        <!-- 為事件綁定別稱時(shí)不要使用駝峰命名 -->
        <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(){//進(jìn)行數(shù)據(jù)的綁定,記住組件內(nèi)的數(shù)據(jù)是一個(gè)方法
        return{
          user:'',
          content:''
        }
      },
      template:"#temp",
      methods:{
        add(){//評論添加函數(shù)
          //獲取當(dā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='';//清空評論列表
          //利用$emit()方法來調(diào)用父組件的方法
          this.$emit('plocalcoments');
        }
      }
    }
    let vm = new Vue({
      el:"#app",
      data:{
        list:[]
      },
      components:{
        box:commentBox
      },
      created(){
        //實(shí)例創(chuà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>簡易評論列表</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">評論人: {{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(){//進(jìn)行數(shù)據(jù)的綁定,記住組件內(nèi)的數(shù)據(jù)是一個(gè)方法
        return{
          user:'',
          content:''
        }
      },
      template:"#temp",
      methods:{
        add(){//評論添加函數(shù)
          //獲取當(dā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='';//清空評論列表
          //利用$emit()方法來調(diào)用父組件的方法
          this.$emit('plocalcoments');
        }
      }
    }
    let vm = new Vue({
      el:"#app",
      data:{
        list:[]
      },
      components:{
        box:commentBox
      },
      created(){
        //實(shí)例創(chuàng)建后加載評論
        this.localComents();
      },
      methods:{
        localComents(){
          let list = localStorage.getItem('cmts'||'[]');//若不存在cmts則返回空數(shù)組,避免json解析出錯(cuò)
          this.list = JSON.parse(list);//刷新數(shù)據(jù)
        }
      }
    });
  </script>
</html>

效果圖:

vue實(shí)現(xiàn)評論列表功能

總結(jié)

以上所述是小編給大家介紹的vue實(shí)現(xiàn)評論列表功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

網(wǎng)站名稱:vue實(shí)現(xiàn)評論列表功能
當(dāng)前鏈接:http://bm7419.com/article10/goeedo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作網(wǎng)站維護(hù)、外貿(mào)建站網(wǎng)站策劃、標(biāo)簽優(yōu)化、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管