当前位置:网站首页>uni-app之renderjs
uni-app之renderjs
2022-08-04 15:19:00 【qq_45689385】
前言
由于app中要使用地图,以前一直用的都是高德地图,在app中是不能操作dom,所以直接用是不可能的,虽然uni-app有地图组件map,但是用着也一直不顺手,最终发现了renderjs,官网提出,在app-vue环境下,视图层由webview渲染,而renderjs运行在视图层,自然可以操作dom和window。
使用
:prop 传值 :change:prop 监听prop改变
调用改变的方法内有四个参数
newValue:跟新后的数据
oldValue:原先的数据
ownerInstance:通过该函数可以调用内部函数,可以传输数据
instance:当前service层实例
通过 this.$ownerInstance.$vm 获取当前组件的 ComponentDescriptor 实例
<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]
}]
}
}
},
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>
注意事项
不可以使用 App、Page 的生命周期
APP 端可以使用 dom、bom API,不可直接访问逻辑层数据,不可以使用 uni 相关接口(如:uni.request)
H5 端逻辑层和视图层实际运行在同一个环境中,相当于使用 mixin 方式,可以直接访问逻辑层数据。所以命名要注意区分
边栏推荐
猜你喜欢

Byte、Short、Integer、Long内部缓存类的对比与源码分析

从-99打造Sentinel高可用集群限流中间件

快速整明白Redis中的字典到底是个啥

附加:自定义注解(参数校验注解);(写的不好,别看…)

1403. Minimum Subsequence in Non-Increasing Order

Leetcode: 215 disorderly to find the first big k element in the array

leetcode: 241. Designing precedence for arithmetic expressions

大众点评搜索相关性技术探索与实践

HarePoint Analytics for SharePoint Online

Semaphore 基本原理
随机推荐
实战:10 种实现延迟任务的方法,附代码!
leetcode: 253. How many meeting rooms are required at least
Hangzhou electric the competition team arrangement (ACM)
Redis 高可用
SublimeText 粘贴图片保存到本地
杭电校赛(逆袭指数)
Legal education combined with VR panorama, intuitively feel and learn the spirit of the rule of law
1403. 非递增顺序的最小子序列
你一定从未看过如此通俗易懂的YOLO系列(从v1到v5)模型解读
Game network UDP + FEC + KCP
I/O stream summary
Next -21- 添加相册系列 - 1- 框架设置
Codeforces Round #811 A~F
附加:自定义注解(参数校验注解);(写的不好,别看…)
Oracle database user creation, restart, import and export
Next -19- 开启fancybox查看图片大图
聊聊与苹果审核员的爱恨情仇
界面组件DevExpress ASP.NET Core v22.1 - 增强数据导出功能
RSA306B,500,600系列API接口代码
多线程编程之优先级翻转问题