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

Vue 之孫組件向爺組件通信的實(shí)現(xiàn)

 更新時間:2019年04月23日 14:26:20   作者:TaoWu  
這篇文章主要介紹了Vue 之孫組件向爺組件通信的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

如何把孫組件內(nèi)特定的數(shù)據(jù)傳給爺組件?

例如

把孫組件的名字傳給爺組件并在爺組件上顯示

思路

  • 在孫組件被點(diǎn)擊后 emit 事件 1 和孫組件的名字
  • 在父組件上監(jiān)聽孫組件 emit 出的事件 1,再次 emit 事件 2
  • 在爺組件上監(jiān)聽父組件 emit 出的事件 2,并觸發(fā)處理函數(shù)
  • 這個處理函數(shù)將父組件傳出的子組件名字顯示在父組件上
<!DOCTYPE html>
<html lang="en" dir="ltr">
 <head>
  <meta charset="utf-8">
  <title></title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.6.7/dist/vue.js"></script>
 </head>
 <body>
  <div id="app">
   child name: {{child}}
   <parent v-on:say2='greeting("child", $event)'></parent>
  </div>
 </body>
 <script>
  Vue.component('parent', {
   template: `
   <div>
    <child v-on:say1='$emit("say2", $event)'></child>
   </div>   `
  })
  Vue.component('child', {
   template: `
    <div>
     child
     <button v-on:click='$emit("say1", "Jack")'>greeting</button>
    </div>
   `
  })
  new Vue({
   el: '#app',
   data: {
    child: '',
   },
   methods: {
    greeting: function (key, value) {
     this[key] = value
    },
   }
  })
 </script>
</html>

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

相關(guān)文章

最新評論