vue中如果父組件想調用子組件的方法,可以在子組件中加上ref,然后通過this.$refs.ref.method調用,例如:
父組件:
<template> <div @click="fatherMethod"> <child ref="child"></child> </div> </template> <script> import child from '~/components/dam/child.vue'; export default { components: { child }, methods: { fatherMethod() {this.$refs.child.childMethods(); } } }; </script>
子組件:
<template> <div>{{name}}</div> </template> <script> export default { data() { return { name: '測試' }; }, methods: { childMethods() { console.log(this.name); } } }; </script>
在父組件中, this.$refs.child 返回的是一個vue實例,可以直接調用這個實例的方法,嗯,就醬~