vue组件父子间通信之综合练习(聊天室)

 更新时间:2017年11月07日 11:17:06   作者:匿名的girl  
这篇文章主要为大家详细介绍了vue组件父子间通信之综合练习聊天室制作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 解决Vue3使用Element-Plus导航刷新后active高亮消失的问题

    解决Vue3使用Element-Plus导航刷新后active高亮消失的问题

    这篇文章主要给大家介绍了如何解决Vue3使用Element-Plus导航刷新后active高亮消失的问题,文中有相关的代码讲解,需要的朋友可以参考下
    2023-08-08
  • 基于mpvue的简单弹窗组件mptoast使用详解

    基于mpvue的简单弹窗组件mptoast使用详解

    这篇文章主要介绍了基于mpvue的简单弹窗组件mptoast使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • vue.js滚动条插件vue-scroll的基本用法

    vue.js滚动条插件vue-scroll的基本用法

    在移动端或PC,页面的部分内容常常需要我们让其在页面滚动,这篇文章主要给大家介绍了关于vue.js滚动条插件vue-scroll的基本用法,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-12-12
  • Vue.js实战之利用vue-router实现跳转页面

    Vue.js实战之利用vue-router实现跳转页面

    对于单页应用,官方提供了vue-router进行路由跳转的处理,这篇文章主要给大家介绍了Vue.js实战之利用vue-router实现跳转页面的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-04-04
  • vue3中不支持.sync语法糖的解决方法

    vue3中不支持.sync语法糖的解决方法

    在 Vue 3 中,.sync 修饰符已经被移除,在 Vue 2 中,.sync 修饰符是一个语法糖,用于简化子组件和父组件之间的双向数据绑定,那么本文将给大家介绍一下vue3中不支持.sync语法糖的解决方法,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-01-01
  • vue实现点击追加选中样式效果

    vue实现点击追加选中样式效果

    今天小编就为大家分享一篇vue实现点击追加选中样式效果,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • vue-element换肤所有主题色和基础色均可实现自主配置

    vue-element换肤所有主题色和基础色均可实现自主配置

    这篇文章主要介绍了vue-element换肤所有主题色和基础色均可实现自主配置,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • 解决vue中reader.onload读取文件的异步问题

    解决vue中reader.onload读取文件的异步问题

    这篇文章主要介绍了解决vue中reader.onload读取文件的异步问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • vue键盘事件点击事件加native操作

    vue键盘事件点击事件加native操作

    这篇文章主要介绍了vue键盘事件点击事件加native操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • vue项目中loadsh库常用方法说明

    vue项目中loadsh库常用方法说明

    这篇文章主要介绍了vue项目中loadsh库常用方法说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11

最新评论