一般多選的樹形組件,使用getCheckedNodes()方法只能獲取到本級的菜單id,只有在子菜單全部選中的情況下才會選中上級。但我們想要不全選中子級的情況下也要獲取它的上級,甚至上上級等,怎么辦呢?
需要改一下node_modules中的源碼:文件路徑為node_modules\element-ui\lib\element-ui.comment.js
搜索文中的TreeStore.prototype.getCheckedNodes方法,改為:
if ((child.checked || child.indeterminate) && (!leafOnly || leafOnly && child.isLeaf)) { checkedNodes.push(child.data) }
就ok啦!
在代碼里仍然是this.$refs.tree.getCheckedNodes(),這樣使用。