当前位置:网站首页>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 方式,可以直接访问逻辑层数据。所以命名要注意区分
边栏推荐
猜你喜欢
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
【北亚数据恢复】IBM System Storage存储lvm信息丢失,卷访问不了的数据恢复方案
MySQL优化学习笔记
Redis-主从复制
2022杭电多校4
《分布式云最佳实践》分论坛,8月11日深圳见
IP第十六天笔记
Http-Sumggling缓存漏洞分析
leetcode:250. 统计同值子树
Bluetooth Technology|In the first half of the year, 1.3 million charging piles were added nationwide, and Bluetooth charging piles will become the mainstream of the market
随机推荐
卖家寄卖流程梳理
leetcode: 251. Expanding 2D Vectors
Find My Technology | Prevent your pet from getting lost, Apple Find My technology can help you
大众点评搜索相关性技术探索与实践
Codeforces Round #811 A~F
IP第十七天笔记
剑指Offer 63.股票的最大利润
PTA 6-2 多项式求值
从-99打造Sentinel高可用集群限流中间件
leetcode:241. 为运算表达式设计优先级
C# 将dll打包到程序中
RTC 场景下的屏幕共享优化实践
leetcode: 241. Designing precedence for arithmetic expressions
你以为在做的是微服务?不!你做的只是分布式单体!
leetcode:250. 统计同值子树
RSA306B,500,600系列API接口代码
推荐一个鸿蒙即时通讯软件《果聊》
基于数据库实现分布式锁
QT笔记——QUuid了解
1403. 非递增顺序的最小子序列