Estou recebendo o seguinte erro ao acessar a rota /cadastro
core.es5.js:1020 ERROR Error: Uncaught (in promise): Error: No provider for UsuarioComponent!
Error: No provider for UsuarioComponent!
at injectionError (core.es5.js:1169)
at noProviderError (core.es5.js:1207)
Essa minha rota chama o seguinte component:
{ path: 'cadastro', component: CadastroUsuarioComponent },
O meu componente de cadastro de usuário e o seguinte:
import { Component, OnInit } from '@angular/core';
import { UsuarioComponent } from '../usuario/usuario.component';
import { UsuarioService } from '../usuario/usuario.service.component';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
@Component({
selector: 'app-cadastro-usuario',
templateUrl: './cadastro-usuario.component.html',
styleUrls: ['./cadastro-usuario.component.css']
})
export class CadastroUsuarioComponent implements OnInit {
constructor(private usuario: UsuarioComponent, private service: UsuarioService, private router: ActivatedRoute) {
this.usuario = usuario;
this.service = service;
this.router = router;
}
ngOnInit() {
}
cadastrar(){
this.service.cadastrar(this.usuario)
.then(res => {
this.usuario = new UsuarioComponent();
})
.catch(erro => console.log(erro));
}
}
Ele chama o seguinte service:
import { Http, Headers, Response } from '@angular/http';
import { UsuarioComponent } from './usuario.component';
import 'rxjs/add/operator/toPromise';
import { Observable } from 'rxjs';
import { Injectable } from '@angular/core';
@Injectable()
export class UsuarioService {
http: Http;
headers: Headers;
url: string = 'http://localhost:3000/v1/usuarios';
constructor(http: Http) {
this.http = http;
this.headers = new Headers();
this.headers.append('Content-Type', 'application/json');
}
lista(): Promise<UsuarioComponent[]>{
return this.http.get(this.url)
.toPromise()
.then(res => res.json())
.catch();
}
cadastrar(usuario: UsuarioComponent): Promise<any> {
if(usuario._id){
return this.http.put(this.url + '/' + usuario._id, JSON.stringify(usuario),
{ headers: this.headers })
.toPromise()
.then(res => res.json())
.catch();
} else {
return this.http.post(this.url, JSON.stringify(usuario),
{ headers: this.headers })
.toPromise()
.then(res => res.json())
.catch();
}
}
remove(usuario: UsuarioComponent): Observable<Response> {
return this.http.delete(this.url + '/' + usuario._id);
}
buscarPorId(id: string): Observable<UsuarioComponent> {
return this.http
.get(this.url + '/' + id)
.map(res => res.json());
}
}
Essa e minha configuração do modulo principal:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { routing } from './app.routes';
import { HomeComponent } from './home/home.component';
import { PainelComponent } from './painel/painel.component';
import { LoginComponent } from './login/login.component';
import { CadastroUsuarioComponent } from './cadastro-usuario/cadastro-usuario.component';
import { UsuarioComponent } from './usuario/usuario.component';
import { UsuarioService } from './usuario/usuario.service.component';
@NgModule({
declarations: [
AppComponent,
HomeComponent,
PainelComponent,
LoginComponent,
CadastroUsuarioComponent,
UsuarioComponent
],
imports: [
BrowserModule,
routing
],
providers: [UsuarioService],
bootstrap: [AppComponent]
})
export class AppModule { }