177-371-24501(同微信)

在 mpvue 使用 echarts 小程序组件

2022-04-21 管理员

  具体操作

  在 mpvue 使用 echarts 小程序组件。下载echarts-for-weixin。

  把其ec-canvas目录移动到 mpvue 项目的static目录下。

  对ec-canvas/ec-canvas.js进行小调整,考虑提 pr 到 ec-canvas。

  修改 ready 为异步获取数据。

  ready: function () {

  // 异步获取

  setTimeout(() => {

  if (!this.data.ec) {

  console.warn('组件需绑定 ec 变量,例:');

  return;

  }

  if (!this.data.ec.lazyLoad) {

  this.init();

  }

  }, 10)

  }

  为 init 添加接收 options 传参

  var query = wx.createSelectorQuery().in(this);

  query.select('.ec-canvas').boundingClientRect(res => {

  if (typeof callback === 'function') {

  this.chart = callback(canvas, res.width, res.height);

  }

  else if (this.data.ec && this.data.ec.onInit) {

  this.chart = this.data.ec.onInit(canvas, res.width, res.height);

  }

  else if (this.data.ec && this.data.ec.options) {

  // 添加接收 options 传参

  const ec = this.data.ec

  function initChart(canvas, width, height) {

  const chart = echarts.init(canvas, null, {

  width: width,

  height: height

  });

  canvas.setChart(chart);

  chart.setOption(ec.options);

  return chart;

  }

  this.chart = initChart(canvas, res.width, res.height);

  }

  }).exec();

  创建pages/bar页面,目录如下:

  .

  └── pages

  └── bar

  ├── index.vue

  └── main.js

  在 main.js 中引入微信小程序的自定义组件

  import Vue from 'vue'

  import App from './index'

  const app = new Vue(App)

  app.$mount()

  // 添加 config json

  export default {

  config: {

  // 这儿添加要用的小程序组件

  usingComponents: {

  'ec-canvas': '../../../static/ec-canvas/ec-canvas'

  }

  }

  }

  在 app.vue 中添加 options、template 等相关配置

  Build Setup

  # install dependencies

  npm install

  # serve with hot reload at localhost:8080

  npm run dev

  # build for production with minification

  npm run build

  # build for production and view the bundle analyzer report

  npm run build --report

上一篇:React转微信小程序:从React类定义到Component调用
下一篇:微信小程序直播订阅提醒功能组件引入
相关资讯 Releva ntnews
解决方案 Solutions
相关热点 Hot spot
快速提升网站排名的方法
  1. 我们的优势
  2. 我们的实力
  3. 选择我们的理由
咨询电话(微信同号)

177-371-24501(同微信)

豫ICP备17049932号

Copyright © 2017-2022 版权所有 追风建站 Rights Reserved 技术支持:酷微科技

电话咨询 在线咨询 服务项目 SEO优化