欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)注明出處,謝謝!

相關(guān)文章

  • vue的自定義指令傳參方式

    vue的自定義指令傳參方式

    這篇文章主要介紹了vue的自定義指令傳參方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 詳解Vue調(diào)用手機(jī)相機(jī)和相冊(cè)以及上傳

    詳解Vue調(diào)用手機(jī)相機(jī)和相冊(cè)以及上傳

    這篇文章主要介紹了Vue調(diào)用手機(jī)相機(jī)及上傳,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • VUE項(xiàng)目中封裝Echart折線圖的方法

    VUE項(xiàng)目中封裝Echart折線圖的方法

    這篇文章主要為大家詳細(xì)介紹了VUE項(xiàng)目中封裝Echart折線圖的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Vue3使用contenteditable打造定制化輸入

    Vue3使用contenteditable打造定制化輸入

    contenteditable 屬性為網(wǎng)頁開發(fā)者提供了一種靈活的方式來創(chuàng)建可編輯的內(nèi)容區(qū)域,使用戶可以直接在網(wǎng)頁上進(jìn)行內(nèi)容編輯,而無需依賴傳統(tǒng)的輸入框,本文將利用contenteditable打造定制化輸入,感興趣的可以了解下
    2023-12-12
  • vue中$set用法詳解

    vue中$set用法詳解

    在vue中,并不是任何時(shí)候數(shù)據(jù)都是雙向綁定的,解決數(shù)據(jù)沒有被雙向綁定我們可以使用?vm.$set?實(shí)例方法,該方法是全局方法?Vue.set?的一個(gè)別名,這篇文章主要介紹了vue中$set用法詳細(xì)講解,需要的朋友可以參考下
    2022-12-12
  • vue中pinia數(shù)據(jù)一直重復(fù)獲取之前的值的解決方法

    vue中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-04
  • 在vue項(xiàng)目如何使用base64加密

    在vue項(xiàng)目如何使用base64加密

    這篇文章主要介紹了在vue項(xiàng)目如何使用base64加密,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • vue/cli?配置動(dòng)態(tài)代理無需重啟服務(wù)的操作方法

    vue/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-05
  • vue構(gòu)建動(dòng)態(tài)表單的方法示例

    vue構(gòu)建動(dòng)態(tài)表單的方法示例

    這篇文章主要介紹了vue構(gòu)建動(dòng)態(tài)表單的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09
  • vue內(nèi)置組件Transition的示例詳解

    vue內(nèi)置組件Transition的示例詳解

    這篇文章主要介紹了vue內(nèi)置組件Transition的詳解,簡(jiǎn)單地說,就是當(dāng)元素發(fā)生變化,比如消失、顯示時(shí),添加動(dòng)畫讓它更自然過渡,它是vue內(nèi)置組件,不需要引入注冊(cè)就可以直接使用,本文通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09

最新評(píng)論