uniapp renderjs 逻辑层,视图层互相传递数据封装
<template>
<view class="content">
<view :prop="sender" :change:prop="renderMaps.handleAction">
<button @click="renderMaps.viewInit">向逻辑层发送数据</button>
<button @click="luoInif">向视图层发送数据</button>
<button @click="luojiFn">触发逻辑层的方法</button>
</view>
</view>
</template>
<!-- 逻辑层 -->
<script>
export default {
data() {
return {
sender: {
handle: "",
callback: "",
options: null,
rendCallback: "",
},
}
},
created() {
console.log(1)
this.sendMsg("init", "handleFeaturePopup", { id: 10 }, (e) => {
console.log("视图层回调函数执行了,可以在这里做一些逻辑处理", e)
})
},
mounted() {
console.log(2)
},
methods: {
handleItemClick(event) {
const handle = this[event.callback]
handle && handle(event.param)
},
sendMsg(handle, callback, options, rendCallback) {
this.sender = {
handle,
callback,
options,
rendCallback,
}
},
handleFeaturePopup(options) {
console.log("options:", options)
console.log("逻辑层接受到了数据:", options)
},
luoInif() {
this.sendMsg("init", "handleFeaturePopup", { id: 100 }, (e) => {
console.log("视图层回调函数执行了,可以在这里做一些逻辑处理", e)
})
},
},
}
</script>
<!-- 视图层 -->
<!-- <script> -->
<script module="renderMaps" lang="renderjs">
let map = null
export default {
data() {
return {
callback: "",
}
},
mounted() {
console.log(3)
},
created() {
console.log(4)
},
methods: {
handleAction(newValue, oldValue, ownerInstance, instance) {
console.log("逻辑层传递过来的数据", newValue)
const handle = this[newValue.handle]
let options = newValue.options
this.callback = newValue.callback
if (!options) {
options = undefined
}
if (!handle) {
console.info("参数为标记需要执行的方法")
return
}
handle(options, newValue?.rendCallback)
},
init(options, fn) {
console.log("视图层接受到数据了")
fn("11")
},
viewInit() {
console.log("视图层向逻辑层发送数据-------------------------------------------")
this.$ownerInstance.callMethod("handleItemClick", {
callback: "handleFeaturePopup",
param: {
name: "李四",
age:11,
},
})
},
luojiFn(){
console.log("this.callback:",this.callback);
this.$ownerInstance.callMethod("handleItemClick", {
callback: this.callback,
param: {
name: "李四11",
age:11,
},
})
}
},
}
</script>
<style lang="scss" scoped></style>