【问题标题】:Apexchart does not update dynamically using axiosApexchart 不使用 axios 动态更新
【发布时间】:2020-08-11 22:11:44
【问题描述】:

updateSeries 功能似乎不起作用。我不知道为什么它没有。 uChart() 函数确实被调用了。我有以下代码:

<template>
<div>
    <Menu></Menu>
    <h1> Our Home Website! </h1>

    <apexchart ref="chart1" width="500" type="line" :options="options" :series="series"></apexchart>
</div>

从'./Menu'导入菜单 从'axios'导入axios

export default {
    name: 'Home',
    components: {
        'Menu': Menu
    },
    data: function () {
        return {
            options: {
                chart: {
                    height: 350,
                    type: 'bar',
                    id: 'chart'
                },
                dataLabels: {
                    enabled: false
                },
                series: [],
                title: {
                    text: 'Ajax Example',
                },
                noData: {
                    text: 'Loading...'
                }
            }
        }
    },
    mounted: function () {
        this.uChart()
    },
    methods: {
        uChart: function() {
            axios
                .get('http://my-json-server.typicode.com/apexcharts/apexcharts.js/yearly')
                .then(function (response) {
                    this.$refs.chart1.updateSeries([{
                        name: 'Sales',
                        data: response.data
                    }])
                });
            console.log(this.$refs.chart1);
        }
    }

对图表的引用和 JSON 数据的链接一样有效。但图表仍处于“加载”状态: This is how it actually looks like on the website and the errors that I get

【问题讨论】:

标签: vue.js web-applications apexcharts


【解决方案1】:

正如 Estus Flask 在他的评论中提到的那样。这个问题的答案是对“then”参数使用箭头语法,因为它是一个回调。因此正确的函数如下所示:

    methods: {
    uChart: function() {
        axios
            .get('http://my-json-server.typicode.com/apexcharts/apexcharts.js/yearly')
            .then(response => {
                this.$refs.chart1.updateSeries([{
                    name: 'Sales',
                    data: response.data
                }])
            });
        console.log(this.$refs.chart1);
    }
}

有关此主题的更多详细信息,请查看此问题的答案: How to access the correct this inside a callback?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-29
    • 2022-12-11
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    相关资源
    最近更新 更多