原始情况:
// 增加删除
operation(id, index) {
let that = this;
// id为功能序号,type代表添加,删除
store.dispatch('operation', {
id: id,
type: this.type,
index: index
}).then(res => {
that.$parent.tips()
})
},
后来因为需求有变化,需要这个组件进行滑动换屏,所在在这个组件外层嵌套了swiper组件.
最坑的来了,因为这个组件我在多处复用,所以有好几种情况,我就用v-if进行了分别显示.
而这个swiper组件只在其中一种情况中使用了,结果其他几种情况都可以使用,都能调用父组件中的方法,只有这个不行,最后恍然大明白,这个组件里面套用组件,导致子组件变成孙子组件,所以this.$parent失效.
有两种办法解决,如下:
1.改成常用的父组件监听子组件调用,具体使用请看官网
// 增加删除
operation(id, index) {
let that = this;
// id为功能序号,type代表添加,删除
store.dispatch('operation', {
id: id,
type: this.type,
index: index
}).then(res => {
that.$emit('tips', res);
})
},
2.询祖(多少层关系,就用多少个$parent)
// 增加删除
operation(id, index) {
let that = this;
// id为功能序号,type代表添加,删除
store.dispatch('operation', {
id: id,
type: this.type,
index: index
}).then(res => {
that.$parent.$parent.tips()
})
},