Home > Enterprise >  how Can I use Injector (Angular) into String or enum
how Can I use Injector (Angular) into String or enum

Time:02-21

This case below does not work for String and Enum. What is the correct way. This is to reduce the number of parameters in the constructor. I have 10 parameters... The Sonarqube complains

 public nome?: string;
 public situacao?: SituacaoPadraoEnum;

  constructor(injector:Injector     
    ) {
        super();
        this.nome = injector.get<String>(String);
        this.SituacaoPadraoEnum = injector.get<SituacaoPadraoEnum>(SituacaoPadraoEnum);
    }

Thanks for help

CodePudding user response:

I found this way on the internet. ==> injector.get('nome');

Does using the Injector bring any performance to the application? I'm just looking for change because of Sonar.

 public nome?: string;
    public numeroDocumento?: string;
    public numeroTelefone?: string;
    public idKeycloak?: string;
    public tipoPessoa?: TIPO_PESSOA;
    public dataNascimento?: string;
    public situacao?: SituacaoPadraoEnum;
    public endereco?: Endereco;
    public pais?: Pais;
    public email?: Email;

    constructor(injector? : Injector) {    
        super();
        this.nome = injector.get<string>(<any>'nome');
        this.numeroDocumento = injector.get<string>(<any>'numeroDocumento');
        this.numeroTelefone = injector.get<string>(<any>'numeroTelefone');
        this.idKeycloak = injector.get<string>(<any>'idKeycloak');
        this.tipoPessoa = injector.get<TIPO_PESSOA>(<any>'tipoPessoa');
        this.dataNascimento = injector.get<string>(<any>'dataNascimento');
        this.situacao = injector.get<SituacaoPadraoEnum>(<any>'situacao');
        this.endereco = injector.get<Endereco>(Endereco);
        this.pais = injector.get<Pais>(Pais);
        this.email = injector.get<Email>(Email);
    }

CodePudding user response:

Other way...

The code below, is it correct? In Angular this part "const { name, documentNumber, phoneNumber, idKeycloak, PersonType, Birthdate, Situation, } = args" it is being considered useless. I will delete it, right?

export class People extends BaseResourceModel {
  
    public endereco?: Endereco;
    public pais?: Pais;
    public email?: Email;

    constructor(injector?: Injector, args?: { 
        nome?: string, 
        numeroDocumento?: string,
        numeroTelefone?: string,
        idKeycloak?: string,
        tipoPessoa?: TIPO_PESSOA,
        dataNascimento?: string,
        situacao?: SituacaoPadraoEnum
     }) 
    { 
        super(); 

        const { nome, numeroDocumento,numeroTelefone,idKeycloak,
            tipoPessoa, dataNascimento, situacao, } = args; 

        this.endereco = injector.get<Endereco>(Endereco);
        this.pais = injector.get<Pais>(Pais);
        this.email = injector.get(Email);

    }

    static fromJson(jsonData: any): Pessoa {
        return Object.assign(new People(), jsonData);
    }
}
  • Related