TypeError: Cannot read properties of undefined (reading ‘graphDataObj‘)

文章讲述了在Vue组件中使用AntVX6进行思维导图封装时遇到的报错,原因在于props的default属性引用了未定义的`graphDataObj`。作者提供了修改后的代码,通过将default设为一个返回默认对象的函数,解决了这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

报错信息:代码展示:

<!--
 * @Author: zhangdapan
 * @Date: 2023-11-01 10:01:49
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-11-01 10:38:56
 * @Description: antv x6思维导图封装
-->
<template>
  <div id="container"></div>
</template>

<script>
import { Graph } from '@antv/x6';
// antv x6 思维导图
export default {
    name: 'antv-x',
    props: {
        graphData:{
            type: Object,
            default: this.graphDataObj
        }
    },
    data() {
        return {
            graphDataObj: {
                nodes: [
                    {
                    id: 'node1',
                    shape: 'rect', // 使用 rect 渲染
                    x: 100,
                    y: 200,
                    width: 80,
                    height: 40,
                    label: 'hello',
                    },
                    {
                    id: 'node2',
                    shape: 'ellipse', // 使用 ellipse 渲染
                    x: 300,
                    y: 200,
                    width: 80,
                    height: 40,
                    label: 'world',
                    },
                ],
                edges: [
                    {
                    source: 'node1',
                    target: 'node2',
                    },
                ],
            }
        }
    },
    mounted() {
        setTimeout(() => {
            this.getInit()
        }, 0);
    },
    methods: {
        getInit() {
            const graph = new Graph({
                container: document.getElementById('container'),
                width: 500,
                height: 500,
                background: {
                    color: '#fffbe6', // 设置画布背景颜色
                },
            });
            graph.fromJSON(this.graphData)
        }
    }
}
</script>

<style>

</style>

问题出现在访问 graphDataObj 属性时,它被认为是未定义的。

这个问题的原因是在 props 中的 default 属性中,使用了 this.graphDataObj,但是在那个时候 graphDataObj 还没有被定义,所以它是 undefined

为了解决这个问题,可以将 default 属性的值设置为一个函数,然后在函数中返回默认的 graphDataObj 对象。这样可以确保在默认情况下,graphDataObj 被正确地定义和返回。

修改后代码:

    props: {
        graphData:{
            type: Object,
            default() {
                return {
                    nodes: [
                    {
                    id: 'node1',
                    shape: 'rect', // 使用 rect 渲染
                    x: 100,
                    y: 200,
                    width: 80,
                    height: 40,
                    label: 'hello',
                    },
                    {
                    id: 'node2',
                    shape: 'ellipse', // 使用 ellipse 渲染
                    x: 300,
                    y: 200,
                    width: 80,
                    height: 40,
                    label: 'world',
                    },
                ],
                edges: [
                    {
                    source: 'node1',
                    target: 'node2',
                    },
                ],
                }
            }
        }
    },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值