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

vue2.x 父組件監(jiān)聽(tīng)子組件事件并傳回信息的方法

 更新時(shí)間:2017年07月17日 16:07:31   作者:百里登風(fēng)  
本篇文章主要介紹了vue2.x 父組件監(jiān)聽(tīng)子組件事件并傳回信息的方法,具有一定的參考價(jià)值,有興趣的可以了解一下

本文介紹了vue2.x 父組件監(jiān)聽(tīng)子組件事件并傳回信息,分享給大家,希望此文章對(duì)各位有所幫助

利用vm.$emit

1、在父組件中引用子組件

<child @from-child-msg="listenChildMsg"></child >

2、子組件中使用$emit發(fā)送事件

this.$emit('from-child-msg', '這是子組件傳遞的消息');

demo

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }
    input, select {
      height: 30px;
    }
  </style>
</head>
<body>
  <div id="app">
    <child value="name" @msg-from-child="getMsgFromChild"></child>
  </div>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.2.6/vue.min.js"></script>
  <script>
    Vue.component('child', {
      data: function () {
        return {
          val: this.value
        }
      },
      props: ['value'],
      methods: {
        handleClick () {
          this.$emit('msg-from-child', this.val)
        }
      },
      template: `
        <div><input type="text" v-model="val"><button type="button" @click="handleClick">確定</button></div>
      `
    })
    new Vue ({
      el: '#app',
      data: {
        
      },
      methods: {
        getMsgFromChild (v) {
          alert('msg: ' + v)
        }
      }
    })
  </script>
</body>
</html>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論