class 类上的方法
class TvControl {
constructor() {
this._nextId = 1
this.root = new TvNode(this)
this.multiSelectAble = false
this.onNodeCreate = null //默认为null
}
get focusNode() {
let len = this.selectedNodes.length
return len ? this.selectedNodes[len - 1] : null
}
}
页面绑定方法时:
async initCtrls() {
let self = this
self.vItem.multiSelectAble = true
self.vItem.onNodeCreate = self.onItemNodeCreate()//错误方法
self.vItem.onNodeCreate = self.onItemNodeCreate.bind(self) //正确方法
}
},
执行时:
//类上的方法
addChild(title, data, params = null) {
let child = this._createNode(title, data, params)
let allowCreate = this.vControl.isNodeNeedAllowCreate && this.vControl.onNodeAllowCreate ? this.vControl.onNodeAllowCreate(child) : true
if (!allowCreate) return null
this.children.push(child)
if (this.vControl.onNodeCreate) {
this.vControl.onNodeCreate(child, params)//传参
}
return child
}
例子:
执行时