我不确定我能准确地记得旧主机属性是做什么的。但我有这个
module
我写它是为了找到一个元素的宿主
export default function domHost(self) {
let parent = self.parentNode;
while(parent && parent.nodeType !== 11) {
parent = parent.parentNode; //work up the hierarchy
}
return parent ? parent.host : self;
}
我经常使用它向宿主元素添加事件侦听器
像这样:-
connectedCallback() {
super.connectedCallback();
this.domHost = domHost(this);
this.domHost.addEventListener('pas-filelocation-request', this._gotRequest);
}
disconnectedCallback() {
super.disconnectedCallback();
this.domHost.removeEventListener('pas-filelocation-request', this._gotRequest);
}