uniapp中this.$parent失效问题

原始情况:

          // 增加删除
			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()
				})
			},
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue2,使用`this.$parent.$parent.$parent.$refs`可以访问父组件的父组件的父组件的`refs`对象。引用的代码是一个示例,其父组件的`this.$refs["detailForm"]`指向子组件`PDetail`的实例,通过`this.$parent.$parent.$parent.$refs`可以在子组件访问父组件的父组件的`refs`对象。具体来说,在上述代码,父组件`Appe`通过`ref="detailForm"`将子组件`PDetail`引用为`"detailForm"`,然后在子组件可以使用`this.$parent.$parent.$parent.$refs.detailForm`来访问父组件的`refs`对象的`detailForm`属性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue组件 $children,$refs,$parent的使用详解](https://download.csdn.net/download/weixin_38669091/12775877)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [vuethis.$parent、this.$root、this.$children、this.$ref的区别和作用](https://blog.csdn.net/weixin_44684303/article/details/124748102)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值