使用vue-resource

时间:2016-11-25 01:28:41

标签: javascript vue-component vue-resource vue.js

在我的Vue组件中,我有以下数据:

data() {
    return {
        revenueChart: '',
        limit: 12,
        labels: '',
        datasets: ''
    }
},

此外,有一种方法使用 vue-resource 来获取数据:

    fetchData() {
        this.$http.get(this.url, {params: {limit: this.limit}}).then(function (response) {
            this.labels = Object.keys(response.data);
            this.datasets = Object.values(response.data);
        });
    },

如您所见,此方法应更新labelsdatasets,但出于某种原因,它们仍然是空字符串(''),它们仍然具有默认值。

例如,我还有以下方法应该使用这些数据来生成图表:

    generateGraph() {
        var data = {
            labels: this.labels, // HERE
            datasets: [
                {
                    label: "Revenue",
                    backgroundColor: "rgba(75,192,192,0.4)",
                    borderColor: "rgba(75,192,192,1)",
                    data: this.datasets // AND HERE
                }
            ]
        };

        var ctx = this.$refs.canvas.getContext('2d');
        this.revenueChart = new Chart(ctx, {
            type: 'line',
            data: data
        });
    },

最后:

    mounted() {
        this.fetchData();
        console.log(this.labels); // STILL EMPTY !
        console.log(this.datasets); // STILL EMPTY !
        this.generateGraph();
    },

使用fetchData方法this.labels后,我的图表为空,this.datasets仍为空。

以下是完整的组件:

<template>
    <div>
        <label>How Many Days?</label>
        <select v-model="limit">
            <option v-for="n in 12">{{ n }}</option>
        </select>
        <canvas ref="canvas"></canvas>
    </div>

</template>

<script>
    export default {
        props: {
            url: ''
        },

        data() {
            return {
                revenueChart: '',
                limit: 12,
                labels: '',
                datasets: ''
            }
        },

        mounted() {
            this.fetchData();
            console.log(this.labels); // STILL EMPTY !
            console.log(this.datasets); // STILL EMPTY !
            this.generateGraph();
        },

        methods: {
            fetchData() {
                this.$http.get(this.url, {params: {limit: this.limit}}).then(function (response) {
                    this.labels = Object.keys(response.data);
                    this.datasets = Object.values(response.data);
                });
            },

            generateGraph() {
                var data = {
                    labels: this.labels,
                    datasets: [
                        {
                            label: "Revenue",
                            backgroundColor: "rgba(75,192,192,0.4)",
                            borderColor: "rgba(75,192,192,1)",
                            data: this.datasets
                        }
                    ]
                };

                var ctx = this.$refs.canvas.getContext('2d');
                this.revenueChart = new Chart(ctx, {
                    type: 'line',
                    data: data
                });
            }
        }
    }
</script>

1 个答案:

答案 0 :(得分:0)

似乎范围已在vue-resource内更改,因为它未更新vue数据变量,请尝试以下操作:

fetchData() {
    var self = this
    this.$http.get(this.url, {params: {limit: this.limit}}).then(function (response) {
        self.labels = Object.keys(response.data);
        self.datasets = Object.values(response.data);
    });
},

另一种方法是使用this.$set,如文档here中所述。

fetchData() {
    this.$http.get(this.url, {params: {limit: this.limit}}).then(function (response) {
        this.$set(labels, Object.keys(response.data))
        this.$set(datasets, Object.values(response.data))
    });
},

被修改

mounted() {
    this.fetchData();
    console.log(this.labels); // STILL EMPTY !
    console.log(this.datasets); // STILL EMPTY !
    this.generateGraph();
},

您将无法在此处查看数据,因为fetchData是异步函数,因此在执行这些控制台日志时不会填充数据,但您可以在模板中使用这些变量,它们将在填充数据后立即呈现,或者您可以按照注释中的建议使用nextTick来控制日志记录数据。