需求:在调用远程的开关的
思路启发:
elementUI中Tree树形控件点击叶子节点才触发 - 百度文库
简述:设备是放在子组件的,开关的按钮是在父组件的==>采用了事件总线(不细讲);
子组件
<!-- 输入框 -->
<el-input placeholder="输入关键字进行过滤"
size="mini"
suffix-icon="el-icon-search"
v-model="filterText">
</el-input>
<el-tree class="filter-tree scrollBar"
:data="deviceData"
ref="tree"
:props="defaultProps"
accordion
:default-expanded-keys="[1]"
node-key="name"
:highlight-current="true"
:filter-node-method="filterNode"
style="overflow: auto; font-size: 14px"
@node-click="handleNodeClick"></el-tree>
---------------------------------
handleNodeClick (val) {
let node = this.$refs.tree.getNode(val.name)
// 如果设备树的节点的长度等于0,说明就是子节点,并且子节点的层级不是1级和2级根节点
if (node.childNodes.length === 0 && (node.level !== 1 && node.level !== 2)) {
this.poitDevice = node
this.$bus.$emit('openClick', node, true)
} else {
this.poitDevice = {}
this.$bus.$emit('openClick', this.poitDevice, false)
//
}
}
父组件:
mounted(){
this.$bus.$on('openClick', this.clickDevice)
}
补充: (暂不细讲--待更新)
beforeDestroy () {
this.$bus.$off('openClick', this.clickDevice)
},
--------------------------------------
clickDevice (msg, open) {
console.log("我接受到传递来的设备", msg, open);
this.deviceMsg = msg
this.clicOrnot = open
},
---------------------------方法:
OpenclickDevice () {
if (!this.clicOrnot) {
this.$message.info("请先选择设备")
} else {
let data = {
deviceVendor: this.deviceMsg.data.deviceVendor,
id: this.deviceMsg.data.deviceCode,
internal: this.formTime.time
}
remoteOpen(data).then(res => {
if (res.code == 200) {
this.$message.info("远程开门成功")
this.deviceMsg = {} //释放内存
this.clicOrnot = false
}
}).catch(error => {
this.$message.error(JSON.stringify(error))
})
}
},