1、只有H5可以直接使用echarts官方文檔配置開發(fā)
2、app端需要使用renderjs標簽注入echarts組件使用。 module="echarts" lang="renderjs 作為script標簽引入
<script module="echarts" lang="renderjs">
let myChart
export default {
mounted() {
if (typeof window.echarts === 'function') {
this.initEcharts()
} else {
// 動態(tài)引入較大類庫避免影響頁面展示
const script = document.createElement('script')
// view 層的頁面運行在 www 根目錄,其相對路徑相對于 www 計算
script.src = 'static/echarts.js'
script.onload = this.initEcharts.bind(this)
document.head.appendChild(script)
}
},
methods: {
initEcharts() {
myChart = echarts.init(document.getElementById('echarts'))
// 觀測更新的數(shù)據(jù)在 view 層可以直接訪問到
myChart.setOption(this.option)
},
updateEcharts(newValue, oldValue, ownerInstance, instance) {
// 監(jiān)聽 service 層數(shù)據(jù)變更
myChart.setOption(newValue)
},
onClick(event, ownerInstance) {
// 調(diào)用 service 層的方法
ownerInstance.callMethod('onViewClick', {
test: 'test'
})
}
}
}
另外需要在指定view增加H5||APP實現(xiàn),其他環(huán)境不支持。需要支持小程序環(huán)境可以考慮使用ucharts
<!-- #ifdef APP-PLUS || H5 -->
<view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" id="echarts" class="echarts"></view>
<!-- #endif -->
注意:prop=option 只能使用data配置的option,不能使用computed后的屬性,在app端運行會有option為null阻止頁面加載