• 技术文章 >web前端 >js教程

    vue父子组件通信使用方法

    php中世界最好的语言php中世界最好的语言2018-04-14 17:03:36原创665

    这次给大家带来vue父子组件通信使用方法,使用vue父子组件通信的注意事项有哪些,下面就是实战案例,一起来看一下。

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

    组件间通信综合练习:
    (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>
    <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>

    相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

    推荐阅读:

    mint-ui loadmore上拉加载与下拉刷新冲突处理方法

    在ES6里模板字符串使用详解

    以上就是vue父子组件通信使用方法的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:使用方法 通信 组件
    上一篇:移除unique限制的方法 下一篇:Vue.js移动端组件库使用方法
    20期PHP线上班

    相关文章推荐

    • 【活动】充值PHP中文网VIP即送云服务器• 聊聊Angular Route中怎么提前获取数据• 简单了解JavaScript事件的冒泡、委派、绑定和传播• node.js gm是什么• 详细介绍JavaScript中Promise的基本概念及使用方法• webpack是基于node.js的吗
    1/1

    PHP中文网