最近用vue和echars做了两个小页面,遇到一个问题是,在mounted发送请求,和初始化echars图,发现数据出不来,现在记录一下解决方法。
解决:在数据请求的时候,生命周期是不会等待你完成请求在继续走mounted,所有你在mounted初始化echarts的时候,请求可能没完成,所以说拿不到数据显示,你可以直接再mounted请求数据,在请求成功回调里面进行echarts初始化
this.$post('/adminOutAccess/',this.department,this.months).then(response=>{
console.log(response.name)
console.log(response.coverage)
console.log(response.duration)
this.staff = response.name
this.coverage = response.coverage
this.duration = response.duration
this.getData()
})
getData(){
const that = this
const option = {
title:{
text:this.department+" "+this.month+" 月走访信息"
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
crossStyle: {
color: '#999'
}
}
},
toolbox: {
feature: {
dataView: {show: true, readOnly: false},
magicType: {show: true, type: ['line', 'bar']},
restore: {show: true},
saveAsImage: {show: true}
}
},
legend: {
data: ['走访时长', '走访覆盖率']
},
xAxis: [
{
type: 'category',
data: that.staff,
axisPointer: {
type: 'shadow'
}
}
],
yAxis: [
{
type: 'value',
name: '走访时长',
min: 0,
max: 100,
interval: 10,
axisLabel: {
formatter: '{value} h'
}
},
{
type: 'value',
name: '走访覆盖率',
min: 0,
max: 100,
interval: 10,
axisLabel: {
formatter: '{value} %'
}
}
],
series: [
{
name: '走访时长',
type: 'bar',
data: [41.16, 42.20, 9.03, 5.72, 59.63, 31.62, 25.04, 21.83, 49.45, 30.18, 13.59, 23.32, 79.81, 93.15, 38.19, 50.25, 55.52, 0.00, 46.25,
]
// data: this.duration
},
{
name: '走访覆盖率',
type: 'line',
yAxisIndex: 1,
// data: this.coverage,
data: [100.00,
100.00,
100.00,
38.89,
98.63,
98.33,
67.42,
65.26,
100.00,
80.77,
100.00,
100.00,
99.16,
87.39,
84.81,
100.00,
100.00,
0.00,
100.00,
]
}
]
};
this.chart1 = echarts.init(document.getElementById('chart1'))
this.chart1.setOption(option)
// this.chart1.showLoading();
}