vue組件父子間通信之綜合練習(xí)(聊天室)
本文實(shí)例為大家分享了vue組件父子間通信之聊天室的具體代碼,供大家參考,具體內(nèi)容如下
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>組件父子間通信之綜合練習(xí)</title> <script src="js/vue.js"></script> </head> <body> <div id="container"> <p>{{msg}}</p> <chat-room></chat-room> </div> <script> // 創(chuàng)建父組件 Vue.component("chat-room",{ //data屬性中的chatList保存用戶(hù)聊天信息 data:function(){ return{ chatList:[] } }, template:` <div> //假的聊天室 <h1>假的聊天室</h1> <user-component userName="Rose"></user-component> <user-component userName="Jack"></user-component> //顯示用戶(hù)的聊天信息 <ul> <li v-for="tmp in chatList">{{tmp}}</li> </ul> </div> ` }) //創(chuàng)建子組件 Vue.component("user-component",{ props:["userName"], //通過(guò)v-model把用戶(hù)輸入的數(shù)據(jù)保存到userInput數(shù)組 data:function(){ return { userInput:[] } }, methods:{ //把用戶(hù)輸入的數(shù)據(jù)以及用戶(hù)名label信息push給chatList數(shù)組 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">發(fā)送</button> </div> ` }) new Vue({ el:"#container", data:{ msg:"Hello VueJs" } }) </script> </body> </html>
組件間通信綜合練習(xí):
(props down,events up)
有2個(gè)組件:chat-room,user-component
user-component是由label input button構(gòu)成
chat-room是由兩個(gè)user-component和一個(gè)列表構(gòu)成
①在chat-room調(diào)用user-component指定label的名字
②在user-component,
點(diǎn)擊按鈕時(shí),將當(dāng)前用戶(hù)輸入的信息發(fā)送給chat-room組件,chat-room接收到數(shù)據(jù)顯示在列表中
代碼:
<!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("在父組件中接收子組件傳來(lái)的數(shù)據(jù)"+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 () { //觸發(fā)toFatherEvent的事件,把input中 //用戶(hù)輸入的數(shù)據(jù)發(fā)送 this.$emit("sendToFather",this.userName+":"+this.userInput); } }, template:` <div> <label>{{userName}}</label> <input type="text" v-model="userInput"/> <button @click="sendToFather">發(fā)送</button> </div> ` }) new Vue({ el: '#container', data: { msg: 'Hello Vue' } }) </script> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue-pdf實(shí)現(xiàn)文件在線預(yù)覽
這篇文章主要為大家詳細(xì)介紹了vue-pdf實(shí)現(xiàn)文件在線預(yù)覽,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08vue3中單文件組件<script?setup>實(shí)例詳解
<script?setup>是vue3中新引入的語(yǔ)法糖,目的是簡(jiǎn)化使用Composition?API時(shí)冗長(zhǎng)的模板代碼,下面這篇文章主要給大家介紹了關(guān)于vue3中單文件組件<script?setup>的相關(guān)資料,需要的朋友可以參考下2022-07-07element-ui table行點(diǎn)擊獲取行索引(index)并利用索引更換行順序
這篇文章主要介紹了element-ui table行點(diǎn)擊獲取行索引(index)并利用索引更換行順序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02vue3(ts)類(lèi)型EventTarget上不存在屬性value的問(wèn)題
這篇文章主要介紹了vue3(ts)類(lèi)型EventTarget上不存在屬性value的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03uniapp中設(shè)置全局頁(yè)面背景色的簡(jiǎn)單教程
uni-app如何設(shè)置頁(yè)面全局背景色,其實(shí)非常簡(jiǎn)單,一句話(huà)即可,但有時(shí)候也會(huì)踩一些坑,這篇文章主要給大家介紹了關(guān)于uniapp中設(shè)置全局頁(yè)面背景色的相關(guān)資料,需要的朋友可以參考下2023-03-03vue實(shí)現(xiàn)頁(yè)面加載動(dòng)畫(huà)效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)頁(yè)面加載動(dòng)畫(huà)效果,vue頁(yè)面出現(xiàn)正在加載的初始頁(yè)面與實(shí)現(xiàn)動(dòng)畫(huà)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09