项目中用到vue-router
,有组件A和组件B,标签<router-view>
中显示组件A,组件A中有一事件a会让<router-view>
中显示的内容变为组件B,如何在组件A的事件a中调用组件B里面声明的事件b呢?
组件A内的事件a会关闭组件A(基本表单),<router-view>
显示内容变为组件B(单基因遗传...),欲在显示内容变为组件B后立即执行组件B中的事件b
组件B
该功能是否可行?希望能有一个思路...
更新(2/25):
子组件A
<template>
<h1>子组件A</h1>
<button v-on:click="funcA">按钮</button>
</template>
<script>
export default{
methods: {
funcA(){
console.log('组件A-方法执行');
this.$emit('eventCall');
}
}
}
</script>
父组件
<template>
<router-view ref="currentView" v-on:eventCall="callB"></router-view>
</template>
<script>
export default{
methods: {
callB(){
console.log("父组件callB方法触发");
this.$refs.currentView.funcB();
}
}
}
</script>
子组件B
<template>
<h1>子组件B</h1>
</template>
<script>
export default{
methods: {
funcB(){
console.log('组件B-方法执行');
alert("成功!");
}
}
}
</script>
然而此方法在父组件调用ref里面的方法时,当前的ref为组件A,获取不到组件B....尝试失败....
如果只是想调用单纯的没有数据交换的方法,直接import,然后在组件的methods中调用该import出来的对象。不知道有没理解错。