Home Web Front-end JS Tutorial Detailed example of chat room for communication between father and son in vue component

Detailed example of chat room for communication between father and son in vue component

Dec 26, 2017 pm 01:28 PM
components communication

This article mainly introduces in detail the creation of a comprehensive practice chat room for parent-child communication in the Vue component. It has a certain reference value. Interested friends can refer to it. I hope it can help everyone.


<!doctype html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>组件父子间通信之综合练习</title>
 <script src="js/vue.js"></script>
 </head>
 <body>
 <p id="container">
  <p>{{msg}}</p>
  <chat-room></chat-room>
 </p>
 <script>

// 创建父组件
  Vue.component("chat-room",{
  //data属性中的chatList保存用户聊天信息
   data:function(){
    return{
     chatList:[]
    }
   },
   template:`
    <p>
     //假的聊天室
     <h1>假的聊天室</h1>
     <user-component userName="Rose"></user-component>
     <user-component userName="Jack"></user-component>
     //显示用户的聊天信息
     <ul>
      <li v-for="tmp in chatList">{{tmp}}</li>
     </ul>
    </p>
   `
  })
 //创建子组件 
  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:`
    <p>
     <label>{{userName}}</label>
     <input type="text" v-model="userInput"/>
     <button @click="sendChat">发送</button>
    </p>
   `
  })
  new Vue({
   el:"#container",
   data:{
    msg:"Hello VueJs"
   }
  })
 </script>
 </body>
</html>
Copy after login

Comprehensive exercise on inter-component communication:
(props down, events up)
There are 2 components: chat-room, user-component
user-component is composed of label input button
chat-room is composed of two user-component and a list

① Call user-component in chat-room to specify the name of the label
② In user-component,
When the button is clicked, the information entered by the current user is sent to the chat-room component, and chat-room receives the data and displays it in the list

Code:


<!DOCTYPE html>
<html>
<head lang="en">
 <meta charset="UTF-8">
 <script src="js/vue.js"></script>
 <title></title>
</head>
<body>

<p id="container">
 <chat-room></chat-room>
</p>

<script>
 Vue.component('chat-room',{
  methods:{
   recvMsg:function(msg){
    console.log("在父组件中接收子组件传来的数据"+msg);
    this.chatList.push(msg);
   }
  },
 data: function () {
  return {
  chatList:[]
  }
 },
 template:`
  <p>
    <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>
  </p>
  `
 })

 Vue.component('user-component',{
 props:['userName'],
 data: function () {
  return {
  userInput:''
  }
 },
 methods:{
  sendToFather: function () {
  //触发toFatherEvent的事件,把input中
  //用户输入的数据发送
  this.$emit("sendToFather",this.userName+":"+this.userInput);
  }
 },
 template:`
  <p>
  <label>{{userName}}</label>
  <input type="text" v-model="userInput"/>
  <button @click="sendToFather">发送</button>
  </p>
  `
 })
 new Vue({
 el: '#container',
  data: {
  msg: 'Hello Vue'
  }
 })
</script>

</body>
</html>
Copy after login

Related recommendations:

How to develop a chat room with PHP

Write multi-person real-time online chat with Node.js Room

How to create a simple chat room using php websocket

The above is the detailed content of Detailed example of chat room for communication between father and son in vue component. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

New generation of optical fiber broadband technology - 50G PON New generation of optical fiber broadband technology - 50G PON Apr 20, 2024 pm 09:22 PM

New generation of optical fiber broadband technology - 50G PON

How to install the Windows 10 old version component DirectPlay How to install the Windows 10 old version component DirectPlay Dec 28, 2023 pm 03:43 PM

How to install the Windows 10 old version component DirectPlay

Analysis of communication between Vue and server: How to deal with network disconnection Analysis of communication between Vue and server: How to deal with network disconnection Aug 10, 2023 am 10:55 AM

Analysis of communication between Vue and server: How to deal with network disconnection

Angular components and their display properties: understanding non-block default values Angular components and their display properties: understanding non-block default values Mar 15, 2024 pm 04:51 PM

Angular components and their display properties: understanding non-block default values

How to achieve point-to-point communication through PHP and P2P protocol How to achieve point-to-point communication through PHP and P2P protocol Jul 28, 2023 pm 10:13 PM

How to achieve point-to-point communication through PHP and P2P protocol

Nokia plans to sell its device management and service management platform businesses for €185 million Nokia plans to sell its device management and service management platform businesses for €185 million Dec 21, 2023 am 08:07 AM

Nokia plans to sell its device management and service management platform businesses for €185 million

A brief history of broadband Internet technology A brief history of broadband Internet technology Apr 16, 2024 am 09:00 AM

A brief history of broadband Internet technology

How to open the settings of the old version of win10 components How to open the settings of the old version of win10 components Dec 22, 2023 am 08:45 AM

How to open the settings of the old version of win10 components

See all articles