vue中使用echarts渲染不出來(lái)
<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ù):
頁(yè)面:
求大佬指點(diǎn)哪里出問(wèn)題了呢?
你在console
中點(diǎn)擊箭頭查看詳情的時(shí)候是點(diǎn)擊的時(shí)候
求的值,這個(gè)值并不是log
時(shí)求的值。看你的截圖,如果打印的時(shí)候數(shù)據(jù)已經(jīng)返回的話,輸出的就不應(yīng)該是[__ob__: Observer]
,而應(yīng)該是類似于(6)["2018-05-02", "2018-05-03", ..., __ob__: Observer]
。
這樣的寫法本身是存在問(wèn)題的,因?yàn)槟阍?code>mounted的時(shí)候無(wú)法保證請(qǐng)求已經(jīng)回來(lái)了,所以比較簡(jiǎn)單的方式是把created
里面的內(nèi)容放到mounted
里面,然后在請(qǐng)求回來(lái)的時(shí)候調(diào)用this.drawLine
;
而且就目前你展示出來(lái)的需求來(lái)看,沒(méi)有必要把數(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
// ...
}
}
生命周期其實(shí)只是vue創(chuàng)建實(shí)例時(shí)候按順序執(zhí)行的一個(gè)一個(gè)函數(shù),所以是在macrotask里的,是當(dāng)前的執(zhí)行棧,而promise.then會(huì)在請(qǐng)求返回的時(shí)候加入到microtask里面,所以你在mounted里的時(shí)候是還沒(méi)有數(shù)據(jù)的,自然也就無(wú)法畫圖了。
另外說(shuō)一下,axios放在created或者mounted區(qū)別不大,但是有可能數(shù)據(jù)返回的時(shí)候還沒(méi)有渲染完成,microtask是在渲染完成之前的,如果卸載created里的話建議把drawline放在this.$nextTick()里面執(zhí)行,避免獲取不到$refs,當(dāng)然你直接document.getElementById了也就沒(méi)什么關(guān)系了
北大青鳥(niǎo)APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥(niǎo)中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開(kāi)發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_(kāi)發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開(kāi)發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開(kāi)發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開(kāi)發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。