问题

在项目中控制台报下图错误:

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.
对vue模板赋值整个vue实例时报错:Property or method toJSON is not defined

解决

  1. 查看报错详情,在全局文件查找 toJSON 字段,并没有找到项目中有该字段,于是怀疑是vue实例内部问题。
  2. 发现每个报错中都是在single组件中报错的,定位到single组件。
  3. 发现组件内向子组件传入了single整个vue实例,如下图所示:
    对vue模板赋值整个vue实例时报错:Property or method toJSON is not defined
  4. 为了实现相同的功能,便在data重新声明一个属性,并且将实例赋值进去。
    对vue模板赋值整个vue实例时报错:Property or method toJSON is not defined
  5. 修改后代码:
    对vue模板赋值整个vue实例时报错:Property or method toJSON is not defined
  6. 测试后,控制台无报错。

(具体原因有待研究)

参考

Vue错误——Property or method “toJSON” is not defined on the instance but referenced during render.

相关文章: