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>
时间: 2024-08-29 14:01:21

vue组件父子间通信之综合练习--假的聊天室的相关文章

vue组件父子间通信02

三.组件间通信($parent $refs) 父组件要想获取子组件的数据:①在调用子组件的时候,指定ref属性<child-component ref="mySon"></child-component> ②根据指定的引用的名字 找到子组件的实例对象this.$refs.mySon 子组件要想获取父组件的数据:①直接读取this.$parent :::通过this.$refs拿到子组件的数据 <!doctype html> <html>

Vue组件------父子间通信

子组件传递数据 用户已经登录 父组件接收数据 并显示列表,未登录不显示列表 /* 有两个组件,分别是main-component,header-component.main-component是由header-component和一个列表(有5条数据 [100,200,300,400,500]),header-component是由一个h1的标签:'这是页头',有一个数据isUserLogin:true 在渲染main-component时候,读取header-component在挂载完毕之后通

vue组件之间的通信

vue组件间的通信有父--->子.子--->父.非父子之间的通信 虽然我们稍微复杂的项目都用vuex来管理了,但是还是想写一篇关于有父--->子.子--->父.非父子 之间通信的文章.下面通过代码来讲述 父--->子组件间的通信 父级页面: <template> <div id="app"> <Header :parentMsg='parentMsg' > </Header> </div> <

vue组件中的通信

一.组件间的关系 1.父子关系 2.兄弟关系 3.隔代关系 二.组件间的通信方式 1.props 2.$emit/$on 3.VUEX 4.$parent/$children 5.$attrs/$listeners 6.provide/inject 三.通信方式举例 新建了一个过程,采用webpack来管理项目.  方法一:props / $emit 1.props---父组件向子组件传值 子组件:sub1.vue 父组件:app.vue 父组件通过props向下传递给子组件.注:组件中的数据共

父子间通信四 ($dispatch 和 $broadcast用法)

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>js测试父子之间通信</title> <script type="text/javascript" src="lib/boot.js"></script> <style> .box{

Vue组件~父子组件之间的通信(传值)

1.父组件向子组件传参 父组件中通过v-bind对要传参数的绑定:(例如) :dataSourceName:是在子组件中要用到的值 "ctpSaveEchartSetting.dataSourceId":是在父组件中要传的值 子组件中的接收通过props(数据值单项绑定的)进行接收:(例如) 注:在props中有的变量就不能再data中再定义,或通过监测.计算属性直接去改变,因为js中的对象和数组是引用对象,指向同一个内存空间,如果在子组件中改变props中的值会影响到父组件中的值的状

vue组件父子之间相互通信案例

vue组件父子组件之间传递数据

举个栗子: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../Vue.js"></script> <template id="tpl1"> <h3>我是父组件

vue组件之间的通信以及如何在父组件中调用子组件的方法和属性

在Vue中组件实例之间的作用域是孤立的,以为不能直接在子组件上引用父组件的数据,同时父组件也不能直接使用子组件的数据 一.父组件利用props往子组件传输数据 父组件: <div> <child v-bind:my-message="parentMsg"></child>//注意传递参数时要用-代替驼峰命名,HTML不区分大小写 </div> 子组件: Vue.component('child', { // camelCase in Ja