uni-app中引入echarts所遇到的坑

梦里梦外; 2023-07-07 08:08 25阅读 0赞

由于传统web端的echarts使用方式在真机运行时报错,以下是解决方式:

  1. <view :prop="options" :change:prop="echarts.updateEcharts" id="line"></view>

注意:options需要定义在data中,赋值可以在mounted,也可以直接写在data

  1. <script module="echarts" lang="renderjs">
  2. let myChart
  3. export default {
  4. mounted() {
  5. if (typeof window.echarts === 'function') {
  6. this.initEcharts();
  7. } else {
  8. // 动态引入较大类库避免影响页面展示
  9. const script = document.createElement('script')
  10. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  11. script.src = 'static/js/echart/echarts.js';//你的项目中echarts.js的路径
  12. script.onload = this.initEcharts.bind(this);
  13. document.head.appendChild(script);
  14. }
  15. },
  16. methods: {
  17. initEcharts: function() {
  18. // this.chart2 = this.load_echart(this.setChartBar(data2), 'canvasLineBar');
  19. myChart = echarts.init(document.getElementById('line'));
  20. // 观测更新的数据在 view 层可以直接访问到
  21. myChart.setOption(this.options);
  22. },
  23. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  24. // 监听 service 层数据变更
  25. if (myChart !== "") {
  26. myChart.setOption(newValue, true)//如果需要频繁的更新数据且更新图表,建议加上true
  27. }
  28. },
  29. }
  30. }
  31. </script>

hbuilderX内置浏览器运行报错

##1、[system] ReferenceError: plus is not defined
问题不大,别慌。不影响你的工作。plus,需要在移动端可以使用

发表评论

表情:
评论列表 (有 0 条评论,25人围观)

还没有评论,来说两句吧...

相关阅读