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

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

 更新時間:2017年11月07日 11:17:06   作者:匿名的girl  
這篇文章主要為大家詳細介紹了vue組件父子間通信之綜合練習聊天室制作,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

<!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>

// 創(chuàng)建父組件
  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>
   `
  })
 //創(chuàng)建子組件 
  Vue.component("user-component",{
   props:["userName"],
   //通過v-model把用戶輸入的數(shù)據(jù)保存到userInput數(shù)組
   data:function(){
    return {
     userInput:[]
    }
   },
   methods:{
    //把用戶輸入的數(shù)據(jù)以及用戶名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>

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

①在chat-room調(diào)用user-component指定label的名字
②在user-component,
點擊按鈕時,將當前用戶輸入的信息發(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("在父組件中接收子組件傳來的數(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中
  //用戶輸入的數(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>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue-pdf實現(xiàn)文件在線預(yù)覽

    vue-pdf實現(xiàn)文件在線預(yù)覽

    這篇文章主要為大家詳細介紹了vue-pdf實現(xiàn)文件在線預(yù)覽,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • vuex actions傳遞多參數(shù)的處理方法

    vuex actions傳遞多參數(shù)的處理方法

    今天小編就為大家分享一篇vuex actions傳遞多參數(shù)的處理方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • vue樣式穿透 ::v-deep的具體使用

    vue樣式穿透 ::v-deep的具體使用

    這篇文章主要介紹了vue樣式穿透 ::v-deep的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • vue3中單文件組件<script?setup>實例詳解

    vue3中單文件組件<script?setup>實例詳解

    <script?setup>是vue3中新引入的語法糖,目的是簡化使用Composition?API時冗長的模板代碼,下面這篇文章主要給大家介紹了關(guān)于vue3中單文件組件<script?setup>的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • Vue實現(xiàn)瀏覽器端掃碼功能

    Vue實現(xiàn)瀏覽器端掃碼功能

    本文主要介紹,通過使用基于 vue技術(shù)棧的前端開發(fā)技術(shù),在瀏覽器端調(diào)起攝像頭,并進行掃碼識別功能,對識別到的二維碼進行跳轉(zhuǎn)或其他操作處理,對vue瀏覽器掃碼功能的實現(xiàn)代碼感興趣的朋友一起看看吧
    2021-10-10
  • element-ui table行點擊獲取行索引(index)并利用索引更換行順序

    element-ui table行點擊獲取行索引(index)并利用索引更換行順序

    這篇文章主要介紹了element-ui table行點擊獲取行索引(index)并利用索引更換行順序,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • vue3(ts)類型EventTarget上不存在屬性value的問題

    vue3(ts)類型EventTarget上不存在屬性value的問題

    這篇文章主要介紹了vue3(ts)類型EventTarget上不存在屬性value的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • uniapp中設(shè)置全局頁面背景色的簡單教程

    uniapp中設(shè)置全局頁面背景色的簡單教程

    uni-app如何設(shè)置頁面全局背景色,其實非常簡單,一句話即可,但有時候也會踩一些坑,這篇文章主要給大家介紹了關(guān)于uniapp中設(shè)置全局頁面背景色的相關(guān)資料,需要的朋友可以參考下
    2023-03-03
  • vue實現(xiàn)頁面加載動畫效果

    vue實現(xiàn)頁面加載動畫效果

    這篇文章主要為大家詳細介紹了vue實現(xiàn)頁面加載動畫效果,vue頁面出現(xiàn)正在加載的初始頁面與實現(xiàn)動畫效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • 一文帶你深入理解Vue3響應(yīng)式原理

    一文帶你深入理解Vue3響應(yīng)式原理

    響應(yīng)式就是當對象本身(對象的增刪值)或者對象屬性(重新賦值)發(fā)生變化時,將會運行一些函數(shù),最常見的就是render函數(shù),下面這篇文章主要給大家介紹了關(guān)于Vue3響應(yīng)式原理的相關(guān)資料,需要的朋友可以參考下
    2022-11-11

最新評論