Ao executar: GET http://localhost:4200/user/flavio Ocorre erro: GET http://localhost:3000/undefined/photos 404 (Not Found)
app.routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { PhotoListComponent } from './photos/photo-list/photo-list.component';
import { PhotoFormComponent } from './photos/photo-form/photo-form.component';
import { NotFoundComponent } from './errors/not-found/not-found.component';
const routes: Routes = [
{ path: 'user/:userName:', component: PhotoListComponent },
{ path: 'p/add', component: PhotoFormComponent },
{ path: '**', component: NotFoundComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
photo-list.component.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { PhotoService } from '../photo/photo.service';
@Component({
selector: 'app-photo-list',
templateUrl: './photo-list.component.html',
styleUrls: ['./photo-list.component.css']
})
export class PhotoListComponent implements OnInit {
photos = [];
constructor(
private photoService: PhotoService,
private activatedRoute: ActivatedRoute
) { }
ngOnInit(): void {
const userName = this.activatedRoute.snapshot.params.userName;
this.photoService
.listFromUser(userName)
.subscribe(photos => this.photos = photos);
}
}
photo.service.ts
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Photo } from './photo';
const API = 'http://localhost:3000';
@Injectable({ providedIn: 'root' })
export class PhotoService {
constructor(private http: HttpClient) { }
listFromUser(userName: string) {
console.log('userName', userName);
return this.http
// .get<Photo[]>(API + '/' + 'flavio' + '/photos');
.get<Photo[]>(API + '/' + userName + '/photos');
}
}