简体   繁体   English

方法参数angular的传递类

[英]passing class for method parameter angular

@Edit @编辑

i have a table of user can insert a new data or update a item of table, in all cases he create or update in one componente, because are only in max 3 inputs, but he can create or edit a lot of things, how section, marca, and etc. when he acess a item a pass with query parameter what he want create or edit. 我有一个用户表,在任何情况下他都可以在一个componente中创建或更新,因此用户可以插入新数据或更新表的一项,因为最多只能输入3个,但是他可以创建或编辑很多东西, ,marca等,当他访问某项带有查询参数的通行证时,他要创建或编辑的内容。 if is secao or marca for example. 例如,如果是secao或marca。 and actualy i'm using swicth case for se wath data he want create or edit: 实际上我正在使用swicth case来创建或编辑他要编辑的数据:

  this.route.queryParams.subscribe( params =>{
       this.id = params['id'];
       this.nome = params['nome'];
       this.descricao = params['descricao'];
       this.podeFracionar = params['podeFracionar'];
       this.entidade = params['entidade'];

       if(this.id !== undefined) this.atualizar = true;
      });

switch(this.entidade){
     case 'secao':
          if(this.atualizar){
            this.secaoService.update(this.id, this.entidadesForm.value).subscribe(secao => {
              this.location.back()
            },
          erro => console.log(erro))
          }else{
            this.secaoService.save(this.entidadesForm.value).subscribe(secao=>{
              this.location.back()
             },
           erro =>{
             console.log(erro)
           })
          }
          break;

          case 'marca':
          if(this.atualizar){
            this.marcaService.update(this.id, this.entidadesForm.value).subscribe(secao => {
              this.location.back()
            },
          erro => console.log(erro))
          }else{
            this.marcaService.save(this.entidadesForm.value).subscribe(secao=>{
              this.location.back()
             },
           erro =>{
             console.log(erro)
           })
          }
          break;

} }

in all cases i call a service for save or update. 在所有情况下,我都致电服务进行保存或更新。 I can create a service for each case and pass class how parameter in method for call the respective class, or has other form to optimize it? 我可以为每种情况创建一个服务并传递类,方法中的参数如何调用相应的类,或者有其他形式对其进行优化?

@Edit thank you for response my question. @Edit谢谢您回答我的问题。 Now i want know how i can remove this switch case 现在我想知道如何移除此开关盒

This is a great use case for polymorphism. 这是多态性的好用例。 Each of your services should implement an interface , say IPersitThings . IPersitThings说,您的每个服务都应实现一个interface Then you can make your switch just assign that variable: 然后,您可以使您的开关仅分配该变量:

let service: IPeristThings;
switch (serviceType) {
   case 'service1':
      service = this.service1;
      break;
   case 'service2':
      service = this.service2;
      break;
}

// Do stuff with service

Note that its possible there's a better way than a switch to get the appropriate service instance; 注意,可能有比switch获得更好的服务实例更好的方法。 but not enough details in your question to be sure. 但您的问题中没有足够的细节可以确定。

I think that its better if you factorize you service class instead to have two services just create one with the method update(..., ... , type) and save(..., type) you will have an smaller switch event you can create another function like this: 我认为它的更好,如果你因式分解您service类,而不是有两个服务仅仅是创建一个具有方法update(..., ... , type)save(..., type)你将有一个小switch事件您可以创建另一个函数,如下所示:

      case 'secao':
      callRepository('secao')
      break;

      case 'marca':
      callRepository('marca') 
      break;

    callRepository(type){
          if(this.atualizar){
            this.customService.update(this.id, this.entidadesForm.value, type)
            .subscribe(()=> this.location.back()
            ,erro => console.log(erro))
          }else{
            this.customService.save(this.entidadesForm.value, type)
            .subscribe(()=> this.location.back()
            ,erro => console.log(erro))
          }

    }

Even you can delete the switch by passing directly the value that contains secao and marca and use directly the function 即使您可以通过直接传递包含secaomarca的值并直接使用该函数来删除开关

callRepository(varValue);

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

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