vue自己封装一个echart公共组件

x33g5p2x  于2022-03-21 转载在 其他  
字(0.8k)|赞(0)|评价(0)|浏览(384)
  1. <template>
  2. <div class="echarts"></div>
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts'
  6. import {
  7. markRaw
  8. } from 'vue'
  9. export default {
  10. props: {
  11. isUpdate: String,
  12. option: {
  13. type: Object,
  14. default: function () {
  15. return {
  16. type: 'default'
  17. }
  18. }
  19. }
  20. },
  21. watch: {
  22. // 防止配置改变后不更新配置,重新加载配置即可
  23. option: {
  24. deep: true,
  25. handler() {
  26. if (this.chart) {
  27. this.chart.setOption(this.option)
  28. }
  29. }
  30. },
  31. // 自适应大小
  32. isUpdate() {
  33. if (this.chart) {
  34. this.chart.resize()
  35. }
  36. }
  37. },
  38. data() {
  39. return {
  40. chart: null
  41. }
  42. },
  43. mounted() {
  44. this.initChart()
  45. },
  46. methods: {
  47. initChart() {
  48. this.chart = markRaw(echarts.init(this.$el, 'dark')) //获取自身dom元素
  49. // this.chart = echarts.init(this.$el, 'dark')
  50. if (this.option.type != 'default') {
  51. this.chart.setOption(this.option)
  52. }
  53. }
  54. }
  55. }
  56. </script>
  57. <style scoped="scoped">
  58. .echarts {
  59. width: 100%;
  60. height: 100%;
  61. }
  62. </style>

相关文章