简体   繁体   English

使用Typescript装饰器初始化类

[英]Class initialize using typescript decorators

I want to automatically initialize the class when a specific decorator is present above the class, for example: 我想在类上方存在特定装饰器时自动初始化该类,例如:

@apiController
export class usersControllers extends lib.baseClasses.apiControllerBase().apiController {
    @lib.decorators.routesRegister().register({ "url": "/login","isLoginUrl":true})
    public postLoginUser($data) {
        let userDetails = {
            name: 'pranay dutta',
            userType: '1', // must
            age: '26',
            tokenExpiry:5000000 // must
        };

        setTimeout(() => {
            this.ok(userDetails);
        }, 100);

        return this.promise;
    }
}

In the above example when i use the @apiController decorator it should automatially initilziae the class for me, please help me with the issue 在上面的示例中,当我使用@apiController装饰器时,它应该自动为我初始化该类,请为我提供帮助

I would do something like 我会做类似的事情

function apiController(target: any) 
{
    let obj = new target.constructor();
}

UPDATE. 更新。 To force decorators to fire I use the following trick. 要强制装饰器开火,我使用以下技巧。 Add dummy function in the same module like this: 在同一模块中添加伪函数,如下所示:

export function Seed() { } 

and then just call it in the initialize routines or your system. 然后只需在初始化例程或您的系统中调用它即可。 That will force all decorators from that module to fire. 这将迫使该模块中的所有装饰器触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM