收藏
回答

Property or method "toJSON" is not defined on the

框架类型 问题类型 操作系统 工具版本
小程序 Bug Windows 1.05.2105272

通过 vue-cli 创建 uni-app 项目。在项目中新建任意组件,通过ref获取组件对象报错

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. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

(found in pages/index/index.vue)(env: Windows,mp,1.05.2105272; lib: 2.17.0


具体代码如下

index.vue

<template>
    <view class="content">
        <image class="logo" src="/static/logo.png"></image>
        <view>
            <text class="title">{{ title }}</text>
        </view>
        <Navbar ref="liveBarrage"></Navbar>
    </view>
</template>


<script>
import Navbar from '@/components/Navbar';
export default {
    components: {
        Navbar,
    },
    data() {
        return {
            barrageContextnull,
            title'Hello',
        };
    },
    onLoad() {},
    onShow() {
        // 获取页面数据
        this.barrageContext = this.$refs.liveBarrage;
    },
    methods: {},
};
</script>


<style>
.content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}


.logo {
    height200rpx;
    width200rpx;
    margin200rpx auto 50rpx auto;
}


.text-area {
    display: flex;
    justify-content: center;
}


.title {
    font-size36rpx;
    color#8f8f94;
}
</style>


navbar.vue

<template>
    <view></view>
</template>


<script>
export default {};
</script>


<style></style>


aas

回答关注问题邀请回答
收藏

2 个回答

  • 工号 9527
    工号 9527
    2021-06-02

    请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

    2021-06-02
    有用
    回复 2
  • 梦尋 # Junjie
    梦尋 # Junjie
    2022-11-03

    toJSON 这个函数 是 JSON.stringify 时 会调用的对象, 如果有 则会调用 没有则不会,

    这是 标准的 JavaScript,

    但微信小程序 内值的输出 console.[log,error,w] 时, 实际上也是格式化对象 后输出,

    我们自定义的对象 有的对象会出现 重复引用, let a={v:c}; let c={v:a};, 这种格式化输出 会导致 内存溢出, 微信小程序 如何输出了这种对象 就会抛出

    所以 请在自定义对象中 添加 toJSON 函数, let a={ v:c, toJSON:function(){return {};} },let c=.....;

    以便避免这样的情况

    2022-11-03
    有用
    回复
登录 后发表内容