[Vue warn]: Property or method “toJSON“ is not defined on the instance but referenced during render.

在开发uniapp项目中,有时运行在微信小程序上测试会报错,报错信息是:[Vue warn]: Property or method “toJSON” is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property

1. 问题呈现

微信小程序开发工具上运行时报错,是这样的,如下截图所示
在这里插入图片描述

2. 定位问题

网上查了一下报错信息,大概知道解释的意思,但是没有用,先用排除法,把之前认为写过的可能有导致问题的代码注释掉,再运行试试,最后发现,应该是这里引起的,源代码如下

export default {
	data() {
		return {
			//...
			myComponent:null,
		};
	},
	onReady() {
		
	},
	mounted() {
		this.$nextTick(()=>{
			console.log('myComponent',this.$refs.myComponent);//测试打印正常
			//this.myComponent = this.$refs.myComponent;
		})
	},
}

💡 将上面那一行去掉注释后,运行会导致错误[Vue warn]: Property or method "toJSON" is not defined on the ...

3. 问题解决

经过各种猜测,最后有一个可行的方法试了,发现只要注释某一行即可,代码如下

export default {
	data() {
		return {
			//...
			//myComponent:null,
		};
	},
	onReady() {
		
	},
	mounted() {
		this.$nextTick(()=>{
			console.log('myComponent',this.$refs.myComponent);//测试打印正常
			this.myComponent = this.$refs.myComponent;
		})
	},
}

又发现一个🕷,为什么会导致这样的问题呢,且容我卖个关子…😛,这是绑定变量引起的,是Vue的特征,可有明白❓

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TA远方

谢谢!收到你的爱╮(╯▽╰)╭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值