最近在写了一个封装echarts的dome时 发现了一些问题。
这是我的echarts
<template>
<div id="echart"></div>
</template>
<script>
export default {
name: 'echart',
props: ["option"],
mounted(){
let myChart = this.$echarts.init(document.getElementById('echart'));
myChart.setOption(this.option);
}
}
</script>
<style lang="scss" scoped>
#echart{
width: 100%;
height: 100%;
}
</style>
然后调用echarts
<template>
<el-row class="splicing" :gutter="20">
<el-col :span="12">111</el-col>
<el-col :span="12">
<myechart :option="option"></myechart>
</el-col>
</el-row>
</template>
<script>
import myechart from "@/components/echars.vue";
export default {
name: "splicing",
components: {
myechart,
},
data() {
return {
option: {
xAxis: {
type: "category",
data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
},
yAxis: {
type: "value",
},
series: [
{
data: [820, 932, 901, 934, 1290, 1330, 1320],
type: "line",
},
],
},
};
},
methods: {},
};
</script>
<style lang="scss" scoped>
.splicing {
height: 100%;
> div {
height: 50%;
box-sizing: border-box;
border: 1px solid red;
}
}
</style>
结果:
看来 他出现啦 这时 我想到在这一个页面中重复调用多次 这个echarts
<template>
<el-row class="splicing" :gutter="20">
<el-col :span="12">111</el-col>
<el-col :span="12">
<myechart :option="option"></myechart>
</el-col>
<el-col :span="12">
<myechart :option="option"></myechart>
</el-col>
<el-col :span="12">
<myechart :option="option"></myechart>
</el-col>
</el-row>
</template>
结果还是只有第一次的生效;
这个原因是因为 我们在当前的这个页面中 多次调用了 echarts这个组件
这个echarts是通过 id控制的 也就是 说我们在当前页面出现了 3个id相同的div。然后通过document.getElementById;一直在调用第一个。所以它之后的echarts都没有生效。
找到问题以后 我们只需要把 通过 document控制 改为 vue的 ref就可以了
<template>
<div class="echart" ref="echart"></div>
</template>
<script>
export default {
mounted(){
let myChart = this.$echarts.init(this.$refs.echart);
myChart.setOption(this.option);
}
}
</script>
版权声明:本文为luer_LJS原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。