uni-app renderjs

1. 前言

官方文档写的跟狗屎一样,说了一大堆都没说到重点,这玩意不就是开了个线程操作Dom吗,重点不就2个:

  • renderjs线程的入口和生命周期
  • 线程间如何通信,主线程 -> renderjs线程,或者 renderjs线程 -> 主线程

2. 代码解析

  • 库引入:没有绝对的方法,要么一开始全局引入,要么动态加载
mounted() {
			if (typeof window.echarts === 'function') {
				this.initEcharts()
			} else {
				const script = document.createElement('script')
				script.src = 'static/echarts.js'
				script.onload = this.initEcharts.bind(this)
				document.head.appendChild(script)
			}
		},
  • 初始化echarts渲染:renderjs线程可直接访问主线程数据
initEcharts() {
				myChart = echarts.init(document.getElementById('echarts'))
				// 直接访问主线程的数据
				myChart.setOption(this.option)
			},
  • :prop="option":单独存在本身没有意义,它只是为了驱动后面的:change:prop="echarts.updateEcharts"
<view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" id="echarts" class="echarts"></view>
  • 数据驱动渲染:参数直接拿到被监听的数据(主线程 -> renderjs线程)
updateEcharts(newValue, oldValue, ownerInstance, instance) {
				myChart && myChart.setOption(newValue)
			},
  • renderjs线程 -> 主线程:通过参数ownerInstance调用主线程的方法进行通信
onClick(event, ownerInstance) {
				// 调用 service 层的方法
				ownerInstance.callMethod('onViewClick', {
					test: 'test'
				})
			}

3.官方完整代码

  • 官方代码都有bug,重点这句,因为:change:prop是立即执行,此时如果eahcrts是动态引入的,会导致初始化未完成,myChart为空,导致报错
myChart.setOption(newValue)
// 改为
myChart && myChart.setOption(newValue)
  • 官方代码
<template>
	<view class="content">
		<!-- #ifdef APP-PLUS || H5 -->
		<view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" id="echarts" class="echarts"></view>
		
		<button @click="changeOption">更新数据</button>
		<!-- #endif -->
		<!-- #ifndef APP-PLUS || H5 -->
		<view>非 APP、H5 环境不支持</view>
		<!-- #endif -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				option: {
					title: {
						text: 'ECharts 入门示例'
					},
					tooltip: {},
					legend: {
						data: ['销量']
					},
					xAxis: {
						data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
					},
					yAxis: {},
					series: [{
						name: '销量',
						type: 'bar',
						data: [5, 20, 36, 10, 10, 20]
					}]
				},
				test: "test"
			}
		},
		onLoad() {

		},
		methods: {
			changeOption() {
				const data = this.option.series[0].data
				// 随机更新示例数据
				data.forEach((item, index) => {
					data.splice(index, 1, Math.random() * 40)
				})
			},
			onViewClick(options) {
				console.log(options)
			}
		}
	}
</script>

<script module="echarts" lang="renderjs">
	let myChart
	export default {
		mounted() {
			if (typeof window.echarts === 'function') {
				this.initEcharts()
			} else {
				// 动态引入较大类库避免影响页面展示
				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'))
				// 观测更新的数据在 view 层可以直接访问到
				myChart.setOption(this.option)
			},
			updateEcharts(newValue, oldValue, ownerInstance, instance) {
				// 监听 service 层数据变更
				myChart.setOption(newValue)
			},
			onClick(event, ownerInstance) {
				// 调用 service 层的方法
				ownerInstance.callMethod('onViewClick', {
					test: 'test'
				})
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.echarts {
		margin-top: 100px;
		width: 100%;
		height: 300px;
	}
</style>

4. 最后,我的评价

image

posted @ 2026-06-04 15:12  ---空白---  阅读(5)  评论(0)    收藏  举报