当前位置:网站首页>uni-app中的renderjs使用
uni-app中的renderjs使用
2022-07-31 19:18:00 【study夏羽】
对于UNI APP端的开发而言,由于上并没有document,不能进行相关的DOM操作,同时有关DOM渲染的第三方库(echart、openlayer等)也无法有效的使用,因此官方推出了renderjs方案,来解决上述问题。
renderjs是一个运行在视图层的js
renderjs的主要作用有2个:
大幅降低逻辑层和视图层的通讯损耗,提供高性能视图交互能力
在视图层操作dom,运行for web的js库
renderjs和service层的通信
具体分为三部分:
1.在template中通过用户手动操作触发事件
2.在service层中调用方法
3.在renderjs中调用方法
从renderjs到service层:通过this.$ownerInstance.callMethod()方法可以调用service中的方法,第一个参数是方法名,第二个参数是传过去的参数
使用方式
设置 script 节点的 lang 为 renderjs
script的module的名称可以随便取,module=(值任意,相当于命名空间,之后会根据这个名字调用其中的方法),但是change:参数名称必须和module保持一致,虽然不会阻断renderjs的运行,但是会报错,也会导致无法捕获数据的变化
<script module="test" lang="renderjs">
export default {
mounted() {
// ...
},
methods: {
// ...
}
}
</script>
<template>
<view>
<!-- prop是个名字,可以随意改,注意:change:[name]这两个名字需要相同就行了 -->
<!-- 从service层到renderjs:首先在template中绑定一个service中定义的值,然后在同样的位置增加:change:(属性名)=(renderjs的module名.触发的方法)来实现通信。
简单来说就是service负责数据的更改,通过template监听数据的变化来通知renderjs
this.$ownerInstance.callMethod方法必须通过点击事件执行-->
<!-- msg是要向renderjs发送的数据,testRenderjs 为renderjs模块名称,onChange是renderjs中的监听方法 -->
<view :prop="msg" :change:prop="testRenderjs.onChange" id="renderjs-view">{
{
msg}}</view>
<button @tap="changeMsgFn">点击修改options</button>
<button @tap="testRenderjs.emitData">直接调用renderjs中的emitData方法</button>
</view>
</template>
<script>
// 原先的script,这里被称为service层
export default {
data() {
return {
// 这里存放准备传递给renderjs的数据
msg: "我是service层原来的msg"
}
},
methods: {
// 触发service层 出入renderjs数据改变
changeMsgFn() {
// 修改msg 触发change
this.msg = "msg的值变了"
},
// 接收renderjs发回的数据
acceptDataFromRenderjs(data) {
console.log('从renderjs中接收到的数据', data)
this.msg = data.content
}
}
}
</script>
<!-- testRenderjs 为renderjs模块名称,lang固定写法 -->
<script module="testRenderjs" lang="renderjs">
export default {
data() {
return {
content:"我是来自renderjs的数据"
}
},
created() {
console.log('renderjs初始化完毕')
},
mounted() {
const view = document.getElementById('renderjs-view')
const button = document.createElement('button')
button.innerText = '一个按钮'
view.appendChild(button)
},
methods: {
// 接收逻辑层service层发送的数据
onChange(newValue, oldValue, ownerInstance, instance) {
console.log('service层中的options发生变化')
console.log('新值newValue', newValue)
console.log('旧值oldValue', oldValue)
// ownerInstance和this.$ownerInstance一样,可用来向service层通信
// instance和ownerInstance的区别是:
// instance.$el指向的是触发事件的那个节点;ownerInstance.$el指向当前vue文件中的根节点;
// instance的作用目前尚不明确,官方没有给出用法
},
// 发送数据到service层
emitData(event, ownerInstance) {
// event是事件对象
ownerInstance.callMethod('acceptDataFromRenderjs', {
content: this.content
})
// 或者
/* this.$ownerInstance.callMethod('acceptDataFromRenderjs',{
content:this.content
}) */
// 需要注意的是:只有通过在template中用户手动操作触发renderjs的方法参数是这两个:event, ownerInstance;通过其他方法触发的函数参数不一样
}
}
}
</script>
边栏推荐
- [PIMF] OpenHarmony Thesis Club - Inventory of the open source Hongmeng tripartite library [3]
- 【AcWing】The 62nd Weekly Match 【2022.07.30】
- What's wrong with the sql syntax in my sql
- 学生管理系统第一天:完成登录退出操作逻辑 PyQt5 + MySQL5.8
- linux查看redis版本命令(linux查看mysql版本号)
- 深度学习中的batch(batch size,full batch,mini batch, online learning)、iterations与epoch
- 迁移学习——Domain Adaptation
- 抖音根据关键词取视频列表 API
- Architect 04 - Application Service Encryption Design and Practice
- 华为手机一键开启“维修模式”隐藏所有数据,让手机隐私更加安全
猜你喜欢
Introduction to Audio Types and Encoding Formats in Unity
Jiuqi ny3p series voice chip replaces the domestic solution KT148A, which is more cost-effective and has a length of 420 seconds
广汽本田安全体验营:“危险”是最好的老师
全平台GPU通用AI视频补帧超分教程
iNeuOS工业互联网操作系统,设备运维业务和“低代码”表单开发工具
MySQL---子查询
【PIMF】OpenHarmony 啃论文俱乐部—盘点开源鸿蒙三方库【3】
中文编码的设置与action方法的返回值
2022 Android interview summary (with interview questions | source code | interview materials)
The whole network is on the verge of triggering, and the all-round assistant for content distribution from media people - Rongmeibao
随机推荐
35 MySQL interview questions and diagrams, this is also easy to understand
iNeuOS工业互联网操作系统,设备运维业务和“低代码”表单开发工具
每日练习------随机产生一个1-100之间的整数,看能几次猜中。要求:猜的次数不能超过7次,每次猜完之后都要提示“大了”或者“小了”。
全平台GPU通用AI视频补帧超分教程
ThreadLocal
手把手教你学会部署Nestjs项目
迁移学习——Domain Adaptation
35道MySQL面试必问题图解,这样也太好理解了吧
有一说一,外包公司到底值不值得去?
保证接口数据安全的10种方式
MySQL---Subqueries
Go basic part study notes
The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]
The new telecom "routine", my dad was tricked!
请问我的这段sql中sql语法哪里出了错
浅谈网络安全之算法安全
【码蹄集新手村600题】通向公式与程序相结合
How programmers learn open source projects, this article tells you
MySQL---Basic select statement
MySQL---基本的select语句