亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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的自定義指令傳參方式

    vue的自定義指令傳參方式

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

    詳解Vue調用手機相機和相冊以及上傳

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

    VUE項目中封裝Echart折線圖的方法

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

    Vue3使用contenteditable打造定制化輸入

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

    vue中$set用法詳解

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

    vue中pinia數(shù)據(jù)一直重復獲取之前的值的解決方法

    這篇文章主要介紹了vue中pinia數(shù)據(jù)一直重復獲取之前的值的解決方法,如果想讓pinia數(shù)據(jù)不會重復獲取之前的值需要手動強制觸發(fā) Pinia store 的狀態(tài)更新,文中有詳細的解決方法,需要的朋友可以參考下
    2024-04-04
  • 在vue項目如何使用base64加密

    在vue項目如何使用base64加密

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

    vue/cli?配置動態(tài)代理無需重啟服務的操作方法

    vue-cli是vue.js的腳手架,用于自動生成vue.js+webpack的項目模板,分為vue?init?webpack-simple?項目名和vue?init?webpack?項目名兩種,這篇文章主要介紹了vue/cli?配置動態(tài)代理,無需重啟服務,需要的朋友可以參考下
    2022-05-05
  • vue構建動態(tài)表單的方法示例

    vue構建動態(tài)表單的方法示例

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

    vue內置組件Transition的示例詳解

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

最新評論