如何获取插件中组件的实例
发布于 5 年前 作者 xiulantan 17447 次浏览 来自 问答

如下图所示,腾讯视频给出的文档中,自己实现了获取组件实例的接口。

let txvContext = TxvContext.getTxvContext('txv1') // txv1即播放器组件的playerid值

这个的实现逻辑是什么?

__
__

__
__

1 回复

这个问题放了好久,最近捡起来,已经搞定了。

在插件组件里面获取实例并缓存到公共数组里,外面可以通过插件暴露的api根据id获取这个实例。

//缓存实例

attached:function(e) {

api.attachedContext(this.id, this);

},

//销毁实例

detached:function(e){

api.detachedContext(this.id);

}

#暴露的api接口。

var instance = [];

function getCustomeContext(id) {

return instance[id];

}

function attachedContext(id, obj) {

instance[id] = obj;

}

function detachedContext(id) {

instance[id] = null;

}

module.exports = {

attachedContext: attachedContext,

detachedContext: detachedContext,

getCustomeContext: getCustomeContext

}

回到顶部