国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

vue組件父子間通信之綜合練習(聊天室)

2024-05-06 15:24:45
字體:
來源:轉載
供稿:網友

本文實例為大家分享了vue組件父子間通信之聊天室的具體代碼,供大家參考,具體內容如下

<!doctype html><html> <head> <meta charset="UTF-8"> <title>組件父子間通信之綜合練習</title> <script src="js/vue.js"></script> </head> <body> <div id="container">  <p>{{msg}}</p>  <chat-room></chat-room> </div> <script>// 創建父組件  Vue.component("chat-room",{  //data屬性中的chatList保存用戶聊天信息   data:function(){    return{     chatList:[]    }   },   template:`    <div>     //假的聊天室     <h1>假的聊天室</h1>     <user-component userName="Rose"></user-component>     <user-component userName="Jack"></user-component>     //顯示用戶的聊天信息     <ul>      <li v-for="tmp in chatList">{{tmp}}</li>     </ul>    </div>   `  }) //創建子組件   Vue.component("user-component",{   props:["userName"],   //通過v-model把用戶輸入的數據保存到userInput數組   data:function(){    return {     userInput:[]    }   },   methods:{    //把用戶輸入的數據以及用戶名label信息push給chatList數組    sendChat:function(){     this.$parent.chatList.push(this.userName+":"+this.userInput);     //情況input框     this.userInput =" ";    }   },   template:`    <div>     <label>{{userName}}</label>     <input type="text" v-model="userInput"/>     <button @click="sendChat">發送</button>    </div>   `  })  new Vue({   el:"#container",   data:{    msg:"Hello VueJs"   }  }) </script> </body></html>

組件間通信綜合練習:
(props down,events up)
有2個組件:chat-room,user-component
user-component是由label input button構成
chat-room是由兩個user-component和一個列表構成

①在chat-room調用user-component指定label的名字
②在user-component,
點擊按鈕時,將當前用戶輸入的信息發送給chat-room組件,chat-room接收到數據顯示在列表中

 代碼:

<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <script src="js/vue.js"></script> <title></title></head><body><div id="container"> <chat-room></chat-room></div><script> Vue.component('chat-room',{  methods:{   recvMsg:function(msg){    console.log("在父組件中接收子組件傳來的數據"+msg);    this.chatList.push(msg);   }  }, data: function () {  return {  chatList:[]  } }, template:`  <div>    <h1>假的聊天室</h1>  <ul>   <li v-for="tmp in chatList">   {{tmp}}   </li>  </ul>  <user-component userName="Lucy" @sendToFather="recvMsg"></user-component>  <user-component userName="Merry" @sendToFather="recvMsg"></user-component>  </div>  ` }) Vue.component('user-component',{ props:['userName'], data: function () {  return {  userInput:''  } }, methods:{  sendToFather: function () {  //觸發toFatherEvent的事件,把input中  //用戶輸入的數據發送  this.$emit("sendToFather",this.userName+":"+this.userInput);  } }, template:`  <div>  <label>{{userName}}</label>  <input type="text" v-model="userInput"/>  <button @click="sendToFather">發送</button>  </div>  ` }) new Vue({ el: '#container',  data: {  msg: 'Hello Vue'  } })</script></body></html>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 石狮市| 深水埗区| 呼伦贝尔市| 吉安市| 安化县| 朝阳市| 凤台县| 榆林市| 鹿邑县| 洪雅县| 五家渠市| 临西县| 农安县| 金塔县| 贺兰县| 察雅县| 卫辉市| 句容市| 六盘水市| 遂溪县| 黄浦区| 长宁区| 招远市| 浪卡子县| 云霄县| 阿荣旗| 桐城市| 本溪市| 辉县市| 休宁县| 永济市| 本溪市| 武威市| 西畴县| 龙里县| 东乌珠穆沁旗| 于田县| 吉安县| 临武县| 光山县| 郧西县|