Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 使用属性装饰器初始化类_Angular_Typescript_Decorator - Fatal编程技术网

Angular 使用属性装饰器初始化类

Angular 使用属性装饰器初始化类,angular,typescript,decorator,Angular,Typescript,Decorator,我正在尝试在Angular 7中创建一个属性装饰器,它在set变量上启动一个类 导出类NewDataClass(){ 公共只读状态={loaded:false} 公众问候{ 返回“你好”; } } 并构建一个decorator,该decorator返回带有一些参数的新类 导出函数NStatus(){ 返回函数(目标:对象,键:字符串|符号):任意{ 返回新的NewDataClass(); }; } @组成部分({ 选择器:“应用程序新建”, templateUrl:“./new.compo

我正在尝试在Angular 7中创建一个属性装饰器,它在set变量上启动一个类

导出类NewDataClass(){
公共只读状态={loaded:false}
公众问候{
返回“你好”;
}
}
并构建一个decorator,该decorator返回带有一些参数的新类

导出函数NStatus(){
返回函数(目标:对象,键:字符串|符号):任意{
返回新的NewDataClass();
};
}

@组成部分({
选择器:“应用程序新建”,
templateUrl:“./new.component.n.html”,
样式URL:['./new.component.n.scss']
})
导出类NewcomComponent实现OnInit、OnDestroy{
@NStatus()公共状态:NewDataClass;
}

组件初始化时,
状态
的值应为
newnewdataclass
。“帮助”

您不能直接从装饰师那里执行此操作。在类创建(而不是实例化)时调用装饰器,因此我们所能做的就是更改类本身以满足我们的需要

一个选项是将字段转换为属性,并在getter上实例化值:

function NStatus() {
    return function (target: Object, key: string, desc?: PropertyDescriptor): PropertyDescriptor {
        return {
            get: function (this: Record<string, NewDataClass>) {
                return this["_" + key] || (this["_" + key] = new NewDataClass())
            },
            set: function (this: Record<string, NewDataClass>, value: NewDataClass) {
                this["_" + key] = value
            }
        }
    }
}

class NewComponent {
    @NStatus() public status: NewDataClass;
}

console.log(new NewComponent().status);
很遗憾,从属性描述符截取类构造函数是不可能的

function NStatus() {
    return function <K extends PropertyKey>(target: { ngOnInit?: () => void  }, key: K): void {
        const originalNgOnInit = target.ngOnInit;
        target.ngOnInit = function (this: Record<K, NewDataClass>) {
            // Init the field
            this[key] = new NewDataClass;
            // Call original ngOnInit if any
            if(originalNgOnInit) originalNgOnInit.call(this);
        }
    }
}

class NewComponent {
    @NStatus() public status: NewDataClass;
    ngOnInit () {}
}

const c = new NewComponent()
c.ngOnInit();
console.log(c.status);