Vue中子组件调用父组件的三种方法 |
您所在的位置:网站首页 › 组件方法 › Vue中子组件调用父组件的三种方法 |
Vue中子组件调用父组件的三种方法: 1.直接在子组件中通过“this.$parent.event”来调用父组件的方法。 父组件 import child from './components/childcomponent'; export default { components: { child }, methods: { fatherMethod() { console.log('父组件方法'); } } };子组件 点击按钮 export default { methods: { childMethod() { this.$parent.fatherMethod(); } } };2.子组件用“$emit”向父组件触发一个事件,父组件监听这个事件即可。 父组件 import child from './components/childcomponent' export default { components: { child }, methods: { fatherMethod() { console.log('父组件方法'); } } };子组件 点击按钮 export default { methods: { childMethod() { this.$emit('fatherMethod'); } } };3.父组件把方法传入子组件中,在子组件里直接调用这个方法即可。 父组件 import child from './components/childcomponent'; export default { components: { child }, methods: { fatherMethod() { console.log('父组件方法'); } } };子组件 点击按钮 export default { props: { fatherMethod: { type: Function, default: null } }, methods: { childMethod() { this.fatherMethod(); } } } }; |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |