鍍金池/ 問答/HTML/ VUE如何用父組件的點擊動作,觸發(fā)子組件中的動作

VUE如何用父組件的點擊動作,觸發(fā)子組件中的動作

這個是父組件

<template>
  <div id="Parent">
    <button v-on:click="parentClick">Parent Click</button>
    <ul>
      <location v-for="child in children">
      </location>
    </ul>
  </div>
</template>

<script>
  import child from './Child.vue'
  
  export default {
    components: {
      child
    },
    methods: {
      deployAll () {
        console.log('Parent Click')
        this.$emit('parentClick')
      }
    },
    computed: {
      children () {
        return [
          {
            id: '1',
            name: 'Beijing',
          },
          {
            id: '2',
            name: 'Xiamen',
          }
        ]
      }
    }
  }
</script>

子組件:

<template>
  <li>
    <button v-on:parentClick="childClick">Child Click</button>
  </li>
</template>

<script>
  export default {
    methods: {
      childClick () {
        console.log('Child Click ')
      }
    },
    props: ['child']
  }
</script>

我想讓父組件中的Parent Click觸發(fā)子組件中的Child Click按鈕,或者子組件中的childClick方法也可以。不知道該如何處理

回答
編輯回答
祉小皓

https://codepen.io/ltaoo/pen/...

子組件隨便監(jiān)聽個事件,父組件點擊時觸發(fā)該事件。

或者直接用 ref。

2018年9月17日 22:02