vue,this.$route 报错Property or method "toJSON"

非常简单的一个模板, 想拿一下路由上的参数就会报如下这个错误

clipboard.png

[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.

但是console.log的东西,是可以打印出来的

clipboard.png

地址栏是:
xxx.xxx.com/#/loading?ck=123456&id=123456

下面是我的代码

<template>
    <div class="loadpay">
        <v-header title='加载中...' text='首页' @back="back" ref='header' style="z-index:9">
            <i class="iconfont icon-jiantouzuo" slot='back'></i>
        </v-header>
        <div>
            <img src='../../assets/images/loading.png'>
        </div>
    </div>
</template>
<script>
    import vHeader from 'components/header'
    import {getUrl, getCode} from 'utils/axios'
    export default {
        name: 'loadpay',
        components: {
            vHeader
        },
        mounted () {
            console.log(this.$route)
        },
        methods: {
            back () {
                this.$router.push({
                    path: '/'
                })
            }
        }
    }
</script>
<style lang="scss" scoped>
    @import '~styles/variables.scss';
    .loadpay{
        text-align: center;
        width: 100%;
        img{
            padding-top: 5rem;
            width: 40%;
        }
    }
</style>

求各路大神解决办法.

阅读 11.4k
4 个回答

通过路由获取参数,这种形式this.$route.toJSON是吗?如果是,你需要在上一级跳转的路由,把toJSON这个参数写在路由里面。如下:

 this.$router.push({
    path: '/aaa',
    name: 'aaa',
    params: {
        bb: 'bb',
        cc: 'cc'
    }
  })

后来找到问题了,是因为我在该组件直接console.log(this.$route)
所以会报toJSON没有定义

如果console.log(this.$route.query)就不会报错...不知道为什么.

是不是没有在props中接收啊,我之前有这样的报错,就是因为没有接收

错误报的已经很明显了,vHeader组件里面有一个toJSON没有定义啊

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题