在vue中使用echart
出現(xiàn)問題。
我先寫一了一個(gè)echart
組件在echart.vue
:
<template>
<div :class="className"
:id="id"
:style="{height:height, width:width}"
ref="myEchart">
</div>
</template>
<script>
import echarts from 'echarts';
export default {
props: {
className: {
type: String,
default: ''
},
id: {
type: String,
default: ''
},
width: {
type: String,
default: '100%'
},
height: {
type: String,
default: '250px'
},
option: {
type: Object,
default: null
}
},
data () {
return {
chart: null
};
},
beforeDestroy () {
if (!this.chart) return;
this.chart.dispose();
this.chart = null;
},
watch: {
option: function (val, oldVal) {
if (val) this.initChart();
}
},
methods: {
initChart () {
this.chart = echarts.init(this.$refs.myEchart);
this.chart.setOption(this.option);
}
}
};
</script>
然后我在huic.vue
文件中調(diào)用這個(gè)組件:
<v-echart
:width="'100%'"
:height="'260px'"
:option="options"
></v-echart>
運(yùn)行代碼后出現(xiàn)了警告(不是報(bào)錯(cuò)):Can't get dom width or height
。圖的寬度設(shè)置100%
,頁(yè)面上圖顯示的寬度是100px
,但是如果設(shè)置的寬度是固定寬度,圖像寬度顯示正常,但兩種情況下警告都在,求大神告知這是為什么?
補(bǔ)充:content.vue
引入了huic.vue
,huic.vue
引入了echart.vue
,父組件通過props向子組件傳參。
這個(gè)是切換后顯示的,我在echart文檔上看到關(guān)于resize
的這樣一句話:有時(shí)候圖表會(huì)放在多個(gè)標(biāo)簽頁(yè)里,那些初始隱藏的標(biāo)簽在初始化圖表的時(shí)候因?yàn)楂@取不到容器的實(shí)際高寬,可能會(huì)繪制失敗,因此在切換到該標(biāo)簽頁(yè)時(shí)需要手動(dòng)調(diào)用 resize 方法獲取正確的高寬并且刷新畫布,或者在 opts 中顯示指定圖表高寬。
所以我懷疑是因?yàn)橐婚_始的時(shí)候huice.vue
,這個(gè)組件是display:none
(我使用了v-show
),所以我試圖在watch
中加入this.chart.resize({width: this.width, height: this.height})
,但是依然報(bào)同樣的警告,寬度100%依然無(wú)法體現(xiàn)。resize
這個(gè)函數(shù)應(yīng)該是頁(yè)面寬高發(fā)生變化時(shí)的回調(diào)函數(shù),我tab切換后只是改變了display
, resize
會(huì)觸發(fā)?
watch在created之后就開始監(jiān)聽變化了,在mounted之前,props的option應(yīng)該發(fā)生了一次變化,而這時(shí)你的chartDom還沒渲染好,所以會(huì)出現(xiàn)圖中的報(bào)錯(cuò)。把chart.init操作移至mounted中就行了。還有,不用每次option變動(dòng)都去執(zhí)行init,只要執(zhí)行一下setOption就能重繪了。
另外,你DOM容器變化后才需要resize,比如調(diào)整窗口大小后,DOM寬度變小了,那就執(zhí)行以下resize(這種情況最好配合debounce使用)
還是直接上代碼吧……
watch: {
option: function (val) {
if (val) this.renderChart();
}
},
mounted(){
this.chart = echarts.init(this.$refs.myEchart);
window.addEventListener("resize", this.onResize, false);
},
destroyed(){
window.removeEventListener("resize", this.onResize, false);
},
methods: {
onResize(){
if(this.chart){
this.chart.resize();
}
}
renderChart () {
// this.chart = echarts.init(this.$refs.myEchart);
this.chart.setOption(this.option);
}
}
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(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)開發(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ū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問,美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。