具体操作
修改 ready 为异步获取数据。
ready: function () { // 异步获取 setTimeout(() => { if (!this.data.ec) { console.warn('组件需绑定 ec 变量,例:<ec-canvas ' + 'canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>'); 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();