A常用的装饰器

function AutoUnsub(obs$ = []) {
    return function(constructor: any) {
        const orig = constructor.prototype.ngOnDestroy
        constructor.prototype.ngOnDestroy = function() {
            for(const prop in this) {
                const property = this[prop]
                if(typeof property.unsubscribe === "function" && !obs$.includes(property))
                    obs$.push(property)
            }
            for(const ob$ of obs$) {
                  ob$.unsubscribe()
            }
            orig.apply()
        }
    }
}

  

猜你喜欢

转载自www.cnblogs.com/dming4/p/12821845.html
今日推荐