2019-06-11組件案例——發(fā)表評(píng)論功能

有一個(gè)頁(yè)面,下邊是評(píng)論列表,展示用戶的評(píng)論。在上面有一個(gè)發(fā)表框,有一發(fā)表按鈕,一點(diǎn)擊,就發(fā)展成功,展示到下方的評(píng)論框。存儲(chǔ)用localStorage

  • 第一步,先把布局寫出來(lái)
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>發(fā)表評(píng)論</title>
  <script src="https://cdn.jsdelivr.net/npm/vue"></script>
  <link rel="stylesheet" href="../bootstrap/css/bootstrap.min.css">
</head>
<body>
  <div id="app">
    <ul class="list-group">
      <li class="list-group-item" v-for="(item, index) in list" :key = "item.id">
          <span class="badge">評(píng)論人:{{ item.user }}</span>
          {{ item.content }}
      </li>
    </ul>
  </div>
</body>
</html>
<script>
new Vue({
  el: "#app",
  data: {
    list: [
      {id: Date.now(),user:"李白",content:"天生我才必有用"},
      {id: Date.now(),user:"江小白",content:"勸君更進(jìn)一杯酒"},
      {id: Date.now(),user:"小馬",content:"我行馬,風(fēng)吹草低見(jiàn)牛羊的馬"},
  
    ],
  },
  methods: {
    
  }
});
</script>
渲染出來(lái)啊,哈哈哈,跟著老師寫案例還可以鞏固我的bootstrap,太棒了

接著寫發(fā)表評(píng)論的框

這個(gè)框,咱寫成一個(gè)組件

<comment-box></comment-box>
......
   <template id="comment">
    <div>
      <div class="form-group">
        <label for="">評(píng)論人:</label>
        <input type="text" class="form-control">
      </div>
      <div class="form-group">
        <label for="">評(píng)論內(nèi)容:</label>
        <textarea class="form-control"></textarea>
      </div>
      <div class="form-group">
        <input type="button" class="btn btn-primary" value="發(fā)表評(píng)論">
      </div>
    </div>
  </template>
......
<script>
  var commentBox = Vue.component("comment",{
    template: "#comment"
  });
  new Vue({
     el: "#app",
     data: {
        list: [
          {id: Date.now(),user:"李白",content:"天生我才必有用"},
         {id: Date.now(),user:"江小白",content:"勸君更進(jìn)一杯酒"},
         {id: Date.now(),user:"小馬",content:"我行馬,風(fēng)吹草低見(jiàn)牛羊的馬"},
       ],
    },
   methods: {
   },
   components: {
      commentBox
   }
 });
</script>
image.png

實(shí)現(xiàn)發(fā)表功能

需要使用雙向數(shù)據(jù)綁定,獲取用戶輸入的評(píng)論人和評(píng)論內(nèi)容,再講這些數(shù)組合成一個(gè)object,push到我們數(shù)組list的最后面

<input type="text" class="form-control" v-model="user">
<textarea class="form-control" v-model="content"></textarea>
<input type="button" class="btn btn-primary" value="發(fā)表評(píng)論" @click="postComment">
........
  var commentBox = Vue.component("comment",{
    template: "#comment",
    data: function(){
      return {
        user: "",
        content:"",
      }
    },
    methods: {
      postComment(){
        let object = {id: Date.now(), user: this.user,content: this.content};
        this.list.push(object);
      }
    },
  });
火急火燎地去測(cè)試,出現(xiàn)bug了??,為啥?因?yàn)閘ist是父組件的

有老師帶著寫,就是不一樣

分析發(fā)表評(píng)論的邏輯
  • 1.評(píng)論數(shù)據(jù)存到哪里去,存到localStorage里
  • 2.先組織出一個(gè)最新的評(píng)論數(shù)據(jù)
  • 3.把第二步中得到的評(píng)論對(duì)象保存到localStorage里面去
    • 3.1 localStorage只支持存放字符串?dāng)?shù)據(jù),要先調(diào)用JSON.stringfy序列化
    • 3.2在保存最新的評(píng)論數(shù)據(jù)之前,要先獲取以前的評(píng)論數(shù)據(jù)(String類型),轉(zhuǎn)為一個(gè)數(shù)組對(duì)象(JSON.parse函數(shù)),然后把最新的評(píng)論unshift存到數(shù)組最前面
    • 3.3如果以前沒(méi)有發(fā)表過(guò)的話,JSON.parse的時(shí)候就會(huì)報(bào)錯(cuò)---3.2步會(huì)有一個(gè)潛在的bug,如果localStorage里的評(píng)論數(shù)組轉(zhuǎn)為空或不存在的時(shí)候,則可以返回一個(gè)"[]"讓JSON.parse去轉(zhuǎn)換
  • 3.4把最新的評(píng)論列表數(shù)據(jù)再次調(diào)用JSON.stringfy轉(zhuǎn)為數(shù)組字符轉(zhuǎn),調(diào)用lolcaStorage.setItem()存儲(chǔ)。
    子組件的methods:
    methods: {
      postComment(){
        let comment = {id: Date.now(), user: this.user,content: this.content};
        var list = JSON.parse(localStorage.getItem("cmts") || "[]");
        list.unshift(comment);
        localStorage.setItem("cmts",JSON.stringify(list));
        this.user = this.content = "";
      }
    },
哦耶,數(shù)組已經(jīng)正確的存儲(chǔ)起來(lái)了
最后,我們讓vue自動(dòng)渲染一下頁(yè)面

父組件里邊有刷新頁(yè)面數(shù)據(jù)的方法,但它不知道什么時(shí)候調(diào)用;子組件知道點(diǎn)擊“發(fā)表評(píng)論”按鈕時(shí)刷新頁(yè)面數(shù)據(jù),但它沒(méi)有刷新頁(yè)面數(shù)據(jù)的方法。這里涉及到了父子組件之間的函數(shù)傳遞

<comment-box @fn1="localComments"></comment-box>
......
  var commentBox = Vue.component("comment",{
  ......
    methods: {
      postComment(){
        let comment = {id: Date.now(), user: this.user,content: this.content};
        var list = JSON.parse(localStorage.getItem("cmts") || "[]");
        list.unshift(comment);
        localStorage.setItem("cmts",JSON.stringify(list));
        this.user = this.content = "";
        this.$emit("fn1");
      }
    },
  });
new Vue({
  el: "#app",
......
  methods:{
    localComments(){
      this.list = JSON.parse(localStorage.getItem("cmts") || "[]");
    },
  },
  created() {
    this.localComments();
  },
});
成功了
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標(biāo)準(zhǔn)。 注意:講述HT...
    kismetajun閱讀 28,878評(píng)論 1 45
  • 概要 64學(xué)時(shí) 3.5學(xué)分 章節(jié)安排 電子商務(wù)網(wǎng)站概況 HTML5+CSS3 JavaScript Node 電子...
    阿啊阿吖丁閱讀 9,880評(píng)論 0 3
  • 一:什么是閉包?閉包的用處? (1)閉包就是能夠讀取其他函數(shù)內(nèi)部變量的函數(shù)。在本質(zhì)上,閉包就 是將函數(shù)內(nèi)部和函數(shù)外...
    xuguibin閱讀 10,067評(píng)論 1 52
  • 前端開(kāi)發(fā)面試題 面試題目: 根據(jù)你的等級(jí)和職位的變化,入門級(jí)到專家級(jí),廣度和深度都會(huì)有所增加。 題目類型: 理論知...
    怡寶丶閱讀 2,694評(píng)論 0 7
  • 有很多媽媽都在問(wèn),0基礎(chǔ)孩子怎么做英語(yǔ)啟蒙?我分享下我給孩子做英語(yǔ)啟蒙初期接觸到的一套試聽(tīng)材料,10個(gè)主題的網(wǎng)課資...
    豆媽2011閱讀 368評(píng)論 0 1

友情鏈接更多精彩內(nèi)容