vue.js - Apexchats.js axios 给了我未定义的 Vue
问题描述
我正在尝试使用vue
and从服务器获取数据apexcharts
,但即使在我用 调用数据之后axios
,它也给了我undefined
..
我错过了什么?
模板
<apexchart
ref="chart1"
width="100%"
:options="chartOptions" :series="series">
</apexchart>
来自 url 的数据
{
"pageviews": 1313,
"new_users": 1014
}
脚本
export default {
data: function () {
return {
series: [],
chartOptions: {
chart: {
type: 'donut',
},
colors: ['#01cd49', '#007568'],
labels: ['new', 're'],
}
},
created: function () {
this.getByVisitor()
},
methods: {
getByVisitor() {
const url = 'url';
axios
.get(url)
.then(response => {
this.$refs.chart1.updateSeries([{
name: 'Sales',
data: response.data
}])
})
.catch(error => (this.byVisitor = error.data));
console.log(`---------------this.$refs.chart1`, this.$refs.chart1);
},
}
解决方案
请参阅更新 Vue 图表数据
无需直接调用updateSeries()
图表组件上的方法,因为它能够对series
. 您所要做的就是更新您的series
数据属性
export default {
data: () => ({
series: [], // start with an empty array here
byVisitor: null, // you seem to have missed this one for your error data
chartOptions: {
chart: {
type: 'donut',
},
colors: ['#01cd49', '#007568'],
labels: ['new', 're'],
}
}),
created: function() {
this.getByVisitor()
},
methods: {
async getByVisitor() {
const url = 'url';
try {
const { data } = await axios.get(url)
// now update "series"
this.series = [{
name: "Sales",
data
}]
} catch (error) {
this.byVisitor = error.data
}
},
}
}
推荐阅读
- python - 将类型/类实例作为参数传递给类构造函数?
- python - 如何使用numpy将除前n个值之外的矩阵(二维数组)的所有值清零?
- reactjs - 反应 setState 工作然后变得未定义
- java - java - 如何比较两个txt文件中的元素并将它们合并到一个文件中?
- c# - Password_Verify 哈希密码问题
- python - 什么是 tf.losses.absolute_difference 的替代品
- c - 忽略scanf返回值并重新定义函数;不同的基本类型
- javascript - 从 php 文件中的 Javascript 调用 php 函数
- javascript - 如何使用 v-if 从显示值为 null 的数组对象中进行条件处理
- php - 通过 curl 表单登录和发布参数