uniapp 使用echarts注意事項

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阻止頁面加載

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容