vue中使用echarts渲染不出來
<template>
<div id="app" class="login_area">
<div id="myChart" class="echarts"></div>
</div>
</template>
<script>
export default {
name: 'second',
data () {
return {
dataList:[],
time:[],
datas:[]
}
},
created: function () {
this.$axios.get("xxxx").then(res => {
this.dataList=res.data.result.group;
this.dataList.sort((a,b)=>{
return b.index - a.index
});
for(var value of this.dataList){
this.time.push(value.recharge_day)
this.datas.push(Number(value.mount))
}
});
},
mounted(){
this.drawLine();
},
methods: {
drawLine(){
let that=this;
console.log(that.time)
console.log(that.datas)
let myChart = this.$echarts.init(document.getElementById('myChart'));
myChart.setOption({
color: ['#90bcf3'],
tooltip: {
trigger: 'axis'
},
legend: {
data: ['收益/元'],
left: 20,
top: 20,
textStyle: {
color: ['#90bcf3']
}
},
toolbox: {
show: false,
feature: {
mark: {show: true},
dataView: {show: true, readOnly: false},
magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
restore: {show: true},
saveAsImage: {show: true}
}
},
calculable: true,
xAxis: [
{
type: 'category',
name: '日期',
boundaryGap: false,
data: that.time,
axisLine: {
lineStyle: {
color: ['#90bcf3']
}
},
axisLabel: {
textStyle: {
color: ['#000']
}
}
}
],
yAxis: [
{
type: 'value',
axisLine: {
lineStyle: {
color: ['#90bcf3']
}
},
axisLabel: {
textStyle: {
color: ['#000']
}
}
}
],
series: [
{
name: '收益/元',
type: 'line',
smooth: true,//平滑
stack: '總量',
data: that.datas
}
]
});
}
}
}
</script>
<style scoped>
.echarts{
width: 700px;
height: 300px;
}
</style>
打印數(shù)據(jù):
頁面:
求大佬指點哪里出問題了呢?
你在console中點擊箭頭查看詳情的時候是點擊的時候求的值,這個值并不是log時求的值??茨愕慕貓D,如果打印的時候數(shù)據(jù)已經(jīng)返回的話,輸出的就不應該是[__ob__: Observer],而應該是類似于(6)["2018-05-02", "2018-05-03", ..., __ob__: Observer]。
這樣的寫法本身是存在問題的,因為你在mounted的時候無法保證請求已經(jīng)回來了,所以比較簡單的方式是把created里面的內(nèi)容放到mounted里面,然后在請求回來的時候調(diào)用this.drawLine;
而且就目前你展示出來的需求來看,沒有必要把數(shù)據(jù)存在data里面,畢竟存在data里面還是耗性能的:
mounted () {
this.$axios.get("xxxx").then(res => {
let list = res.data.result.group;
list.sort((a,b)=>{
return b.index - a.index
});
let time = []
let datas = []
for(var value of list){
time.push(value.recharge_day)
datas.push(Number(value.mount))
}
this.drawLine(time, datas)
});
},
methods: {
drawLine (time, datas) { // 替換掉里面的that.time和that.datas
// ...
}
}生命周期其實只是vue創(chuàng)建實例時候按順序執(zhí)行的一個一個函數(shù),所以是在macrotask里的,是當前的執(zhí)行棧,而promise.then會在請求返回的時候加入到microtask里面,所以你在mounted里的時候是還沒有數(shù)據(jù)的,自然也就無法畫圖了。
另外說一下,axios放在created或者mounted區(qū)別不大,但是有可能數(shù)據(jù)返回的時候還沒有渲染完成,microtask是在渲染完成之前的,如果卸載created里的話建議把drawline放在this.$nextTick()里面執(zhí)行,避免獲取不到$refs,當然你直接document.getElementById了也就沒什么關系了
北大青鳥APTECH成立于1999年。依托北京大學優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
達內(nèi)教育集團成立于2002年,是一家由留學海歸創(chuàng)辦的高端職業(yè)教育培訓機構,是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
北大課工場是北京大學校辦產(chǎn)業(yè)為響應國家深化產(chǎn)教融合/校企合作的政策,積極推進“中國制造2025”,實現(xiàn)中華民族偉大復興的升級產(chǎn)業(yè)鏈。利用北京大學優(yōu)質(zhì)教育資源及背
博為峰,中國職業(yè)人才培訓領域的先行者
曾工作于聯(lián)想擔任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責任公司從事總經(jīng)理職務負責iOS教學及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術, 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術功底深厚。 授課風格 授課風格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術顧問,美國Dachieve 系統(tǒng)架構師,美國AngelEngineers Inc. 系統(tǒng)架構師。