最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2019年10月25日 11:09:51   作者:angle-xiu  
本文通過(guò)實(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">
        <!-- 為事件綁定別稱(chēng)時(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()方法來(lái)調(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">
        <!-- 為事件綁定別稱(chēng)時(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()方法來(lái)調(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ì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

  • vue中echarts引入中國(guó)地圖的案例

    vue中echarts引入中國(guó)地圖的案例

    這篇文章主要介紹了vue中echarts引入中國(guó)地圖的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • Vue+Node實(shí)現(xiàn)大文件上傳和斷點(diǎn)續(xù)傳

    Vue+Node實(shí)現(xiàn)大文件上傳和斷點(diǎn)續(xù)傳

    文件上傳在很多項(xiàng)目中都用的到,如果是幾M的很快就傳送完畢,如果是大文件呢?本文將利用Vue+Node實(shí)現(xiàn)大文件上傳和斷點(diǎn)續(xù)傳,感興趣的可以了解一下
    2022-04-04
  • Vite引用本地靜態(tài)資源的正確方式

    Vite引用本地靜態(tài)資源的正確方式

    在前端開(kāi)發(fā)中,除了通過(guò) API 動(dòng)態(tài)請(qǐng)求的數(shù)據(jù)外,還有一些諸如 HTML 文件、圖片、字體等文件需要在項(xiàng)目中被用到,通常這些被視為靜態(tài)資源,本文將基于 Vite + React 來(lái)講述并解鎖 Vite 引用本地靜態(tài)資源的正確姿勢(shì),需要的朋友可以參考下
    2024-12-12
  • vant?toast?關(guān)閉棧溢出問(wèn)題及解決

    vant?toast?關(guān)閉棧溢出問(wèn)題及解決

    這篇文章主要介紹了vant?toast?關(guān)閉棧溢出問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • vue props default Array或是Object的正確寫(xiě)法說(shuō)明

    vue props default Array或是Object的正確寫(xiě)法說(shuō)明

    這篇文章主要介紹了vue props default Array或是Object的正確寫(xiě)法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • 優(yōu)化Vue頁(yè)面中的表單布局和樣式的技巧

    優(yōu)化Vue頁(yè)面中的表單布局和樣式的技巧

    在日常開(kāi)發(fā)中,Vue 項(xiàng)目中的表單布局和樣式優(yōu)化是一個(gè)重要的環(huán)節(jié),通過(guò)合理的布局與美觀的樣式設(shè)計(jì),不僅可以提升用戶(hù)體驗(yàn),還能增加頁(yè)面的實(shí)用性和觀賞性,本文將總結(jié)幾個(gè)常見(jiàn)的表單和表格布局優(yōu)化的技巧,需要的朋友可以參考下
    2024-10-10
  • element-ui?tree?異步樹(shù)實(shí)現(xiàn)勾選自動(dòng)展開(kāi)、指定展開(kāi)、指定勾選功能

    element-ui?tree?異步樹(shù)實(shí)現(xiàn)勾選自動(dòng)展開(kāi)、指定展開(kāi)、指定勾選功能

    這篇文章主要介紹了element-ui?tree?異步樹(shù)實(shí)現(xiàn)勾選自動(dòng)展開(kāi)、指定展開(kāi)、指定勾選,項(xiàng)目中用到了vue的element-ui框架,用到了el-tree組件,由于數(shù)據(jù)量很大,使用了數(shù)據(jù)懶加載模式,即異步樹(shù),需要的朋友可以參考下
    2022-08-08
  • VUE使用vuex解決模塊間傳值問(wèn)題的方法

    VUE使用vuex解決模塊間傳值問(wèn)題的方法

    本篇文章主要介紹了VUE使用vuex解決模塊間傳值問(wèn)題 ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • 基于vue-ssr的靜態(tài)網(wǎng)站生成器VuePress 初體驗(yàn)

    基于vue-ssr的靜態(tài)網(wǎng)站生成器VuePress 初體驗(yàn)

    VuePress為每一個(gè)由它生成的頁(yè)面提供預(yù)加載的html,不僅加載速度極佳,同時(shí)對(duì)seo非常友好。這篇文章主要介紹了基于vue-ssr的靜態(tài)網(wǎng)站生成器VuePress 初體驗(yàn),需要的朋友可以參考下
    2018-04-04
  • Vue?3?進(jìn)階用法之異步組件

    Vue?3?進(jìn)階用法之異步組件

    為了解決加載組件中出現(xiàn)的報(bào)錯(cuò)、超時(shí)、狀態(tài)展示等問(wèn)題,可以使用?Vue?3?提供的異步組件(Async?Components),它對(duì)于加載過(guò)程做了更細(xì)致的控制,這篇文章主要介紹了Vue?3?進(jìn)階用法之異步組件,需要的朋友可以參考下
    2024-04-04

最新評(píng)論

阳东县| 泰安市| 图木舒克市| 厦门市| 平邑县| 兴山县| 固始县| 福安市| 泉州市| 清河县| 府谷县| 修武县| 桦甸市| 崇信县| 金溪县| 军事| 江口县| 平顶山市| 华安县| 宁安市| 青川县| 徐州市| 丹凤县| 云和县| 晋城| 佛坪县| 灌南县| 贡嘎县| 辉县市| 正镶白旗| 彭山县| 阳泉市| 淮南市| 郑州市| 浦县| 调兵山市| 遂溪县| 安龙县| 礼泉县| 曲周县| 顺义区|