当前位置:网站首页>echart简单组件封装
echart简单组件封装
2022-07-06 10:24:00 【bug丸】
echart简单组件封装
<template>
<div class="chartBox" ref="echartBox"></div>
</template>
<script>
import * as echarts from 'echarts'
export default {
props: {
barData: {
type: Array,
default: () => []
}
},
watch: {
barData: {
handler: function() {
this.initChart()
},
deep: true
}
},
mounted() {
this.initChart()
},
methods: {
initChart() {
const dom = this.$refs.echartBox
const myChart = echarts.init(dom)
myChart.setOption({
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
data: this.barData,
type: 'bar'
}
]
})
}
}
}
</script>
<style scoped>
.chartBox {
width: 400px;
height: 300px;
}
</style>
遇到的问题:
封装的echart组件复用时,会警告:
上述代码如果我将this.$ref 改为 document.querySelector;那么浏览器会出来一条警告,而且只会渲染出一个图
警告内容: There is a chart instance already initialized on the dom.
// const dom = this.$refs.echartBox
const dom = document.querySelector('.chartBox')
解决方式:
方式1:this.$refs
如同开始部分的代码
方式2:传入一个唯一的className或id,用来区分
<div class="chartBox" :id="eID"></div>
props: {
eID: {
type: String,
default: ''
}
},
const dom = document.querySelector('#' + this.eID)
边栏推荐
- Automatic reservation of air tickets in C language
- The latest financial report release + tmall 618 double top, Nike energy leads the next 50 years
- Excellent open source fonts for programmers
- declval(指导函数返回值范例)
- 二分(整数二分、实数二分)
- 【.NET CORE】 请求长度过长报错解决方案
- Jerry's watch reads the file through the file name [chapter]
- Windows连接Linux上安装的Redis
- 推荐好用的后台管理脚手架,人人开源
- Easy to use PDF to SVG program
猜你喜欢
Interesting - questions about undefined
Windows connects redis installed on Linux
编译原理——预测表C语言实现
C语言通过指针交换两个数
Ms-tct: INRIA & SBU proposed a multi-scale time transformer for motion detection. The effect is SOTA! Open source! (CVPR2022)...
Jerry's access to additional information on the dial [article]
RB157-ASEMI整流桥RB157
Take you through ancient Rome, the meta universe bus is coming # Invisible Cities
Jerry is the custom background specified by the currently used dial enable [chapter]
Virtual machine VirtualBox and vagrant installation
随机推荐
Cobra 快速入门 - 专为命令行程序而生
C language college laboratory reservation registration system
Why does wechat use SQLite to save chat records?
【剑指 Offer】 60. n个骰子的点数
30 分钟看懂 PCA 主成分分析
Wchars, coding, standards and portability - wchars, encodings, standards and portability
Why should Li Shufu personally take charge of building mobile phones?
Comparative examples of C language pointers *p++, * (p++), * ++p, * (++p), (*p) + +, +(*p)
TOP命令详解
Common - magic number 7
2022暑期项目实训(二)
【Swoole系列2.1】先把Swoole跑起来
二分(整数二分、实数二分)
d绑定函数
Declval of template in generic programming
C语言高校实验室预约登记系统
RB157-ASEMI整流桥RB157
Cocos2d Lua 越来越小样本 内存游戏
declval(指导函数返回值范例)
Olivetin can safely run shell commands on Web pages (Part 1)