Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

94
Visualizações
Adding a new service to Angular project

I've added a new Service to an existing Angular project with:

$ ng generate service utils/new

Now I tried to move some methods from AppService to NewService.

Both services have the same constructor:

@Injectable({
  providedIn: 'root'
})
export class AppService {

  constructor(private http: HttpClient) {}

And

@Injectable({
  providedIn: 'root'
})
export class NewService {

  constructor(private http: HttpClient) { }

Now in a Component I try to use NewService instead of AppService (I simply replace AppService with NewService).

@Component({
//...
})
export class MyComponent implements OnInit {


  constructor(private newService: NewService) {
    newService.doSomething(...);
  }

  ngOnInit(): void {
  }

It compiles. But I get a runtime error: ERROR TypeError: n.appService is undefined

I could not understand what the debugger was saying so I made a guess: I added private appService: AppService to the constructor, although it is not being used at all in the code of MyComponent. So now I have this:

@Component({
//...
})
export class MyComponent implements OnInit {


  constructor(private appService: AppService, private newService: NewService) {
    newService.doSomething(...);
  }

  ngOnInit(): void {
  }

Now it compiles, and I also don't get any runtime error.

This looks strange and counterintuitive to me. What did I miss here?

Do I need some configuration setting to declare the existence of NewService?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This happened because in the html view of MyComponent was a reference to an AppService method. Strangely, the project still compiled and only failed at runtime.

Usually when I refer in html views to entities that are not recognized, I get a compilation error.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda