vue3中使用echarts图表解决父传子 子组件不更新数据

443 阅读2分钟

先写一个简单的父传子

<template>
  <div>
    <echarts :color="color"></echarts>
    <button @click="edit">修改</button>
  </div>
</template>

<script setup>
import { ref } from "vue"
import echarts from "./components/echarts.vue";
const color = ref(['#333', '#666', '#999', '#ccc', '#eee'])
const edit = () => {
  color.value = ['black', 'white', 'red', 'green', 'yellow']
}
</script>

子组件中引入defineProps接收父组件传递的数据

import { ref, onMounted, defineProps, watch, toRef } from "vue"
import * as echarts from 'echarts'
const props = defineProps({
    color: {
        type: Array
    }
})

通过toRef把接收到数据转换成响应式数据

import { ref, onMounted, defineProps, watch, toRef } from "vue"
const colors = toRef(props, 'color')

通过watch监听接收的数据是否发生变化

watch(colors, (newVal, oldVal) => {
    if (newVal) {
        options.value.color = newVal
        renderChart()
    } else {
        console.log('没有变化');
    }
})

子组件定义options

const options = ref(
    {
        legend: {
            data: ['Java', 'C', 'C++', 'Python', 'C#']
        },
        color: props.color,
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        toolbox: {
            feature: {
                saveAsImage: {}
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: ['1989', '1999', '2009', '2019']
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                name: 'Java',
                type: 'line',
                stack: 'Total',
                data: [120, 132, 101, 134, 90, 230, 210]
            },
            {
                name: 'C',
                type: 'line',
                stack: 'Total',
                data: [220, 182, 191, 234, 290, 330, 310]
            },
            {
                name: 'C++',
                type: 'line',
                stack: 'Total',
                data: [150, 232, 201, 154, 190, 330, 410]
            },
            {
                name: 'Python',
                type: 'line',
                stack: 'Total',
                data: [320, 332, 301, 334, 390, 330, 320]
            },
            {
                name: 'C#',
                type: 'line',
                stack: 'Total',
                data: [820, 932, 901, 934, 1290, 1330, 1320]
            }
        ]
    }
)

子组件创建实例+画图

// 1. 创建echarts实例
let mChart = null;
const target = ref(null)

onMounted(() => {
    mChart = echarts.init(target.value, 'dark')
    renderChart()
})
// 2. 构建 option 配置对象
const renderChart = () => {
    // 3. 通过 实例.setOptions(option) 方法加载配置
    mChart.setOption(options.value)
}

完整代码

父组件
<template>
  <div>
    <echarts :color="color"></echarts>
    <button @click="edit">修改</button>
  </div>
</template>

<script setup>
import { ref } from "vue"
import echarts from "./components/echarts.vue";
const color = ref(['#333', '#666', '#999', '#ccc', '#eee'])
const edit = () => {
  color.value = ['black', 'white', 'red', 'green', 'yellow']
}
</script>
子组件
<template>
    <div class="wrapper">
        <div ref="target" style="width: 100%;height: 300px"></div>
    </div>
</template>

<script setup>
import { ref, onMounted, defineProps, watch, toRef } from "vue"
import * as echarts from 'echarts'
const props = defineProps({
    color: {
        type: Array
    }
})

const colors = toRef(props, 'color')

watch(colors, (newVal, oldVal) => {
    if (newVal) {
        options.value.color = newVal
        renderChart()
    } else {
        console.log('wc');
    }
})


const options = ref(
    {
        legend: {
            data: ['Java', 'C', 'C++', 'Python', 'C#']
        },
        color: props.color,
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        toolbox: {
            feature: {
                saveAsImage: {}
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: ['1989', '1999', '2009', '2019']
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                name: 'Java',
                type: 'line',
                stack: 'Total',
                data: [120, 132, 101, 134, 90, 230, 210]
            },
            {
                name: 'C',
                type: 'line',
                stack: 'Total',
                data: [220, 182, 191, 234, 290, 330, 310]
            },
            {
                name: 'C++',
                type: 'line',
                stack: 'Total',
                data: [150, 232, 201, 154, 190, 330, 410]
            },
            {
                name: 'Python',
                type: 'line',
                stack: 'Total',
                data: [320, 332, 301, 334, 390, 330, 320]
            },
            {
                name: 'C#',
                type: 'line',
                stack: 'Total',
                data: [820, 932, 901, 934, 1290, 1330, 1320]
            }
        ]
    }
)

// 1. 创建echarts实例
let mChart = null;
const target = ref(null)

onMounted(() => {
    mChart = echarts.init(target.value, 'dark')
    renderChart()
})
// 2. 构建 option 配置对象
const renderChart = () => {
    // 3. 通过 实例.setOptions(option) 方法加载配置
    mChart.setOption(options.value)
}

</script>

<style lang="less" scoped>
.wrapper {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;

    .header {
        height: 50px;
        background: #4398d1;
        color: #fff;
        font-size: 20px;
        display: flex;
        align-items: center;
    }

    .target {
        flex: 1;
    }
}
</style>