After creating an Angular App, I encountered a challenge in one of my services. When I call the http.post method and subscribe to it, I aim to redirect to the previous page with a parameter (e.g., "http://localhost:3000/profile/aRandomName"). Unfortunately, my attempts to use the router method have been unsuccessful.
The following code snippet depicts part of my service:
constructor(private http: HttpClient, private router: Router){}
updatePositiveRanked(thisUserUsername:string, rateValue:string) {
const updatePositiveData = {
thisUserUsername: thisUserUsername,
rateValue:rateValue
}
this.http.post<{message:string}>("http://localhost:3000/api/profile/positiveRank", updatePositiveData)
.subscribe(response => {
console.log(response.message);
this.router.navigate(['profile/'+thisUserUsername]);
})
}
Below is the content of my app-routing.module.ts file:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { PostListComponent } from './posts/posts-list/posts-list.component';
import { PostCreateComponent } from './posts/post-create/post-create.component';
import {AuthGuard} from './auth/auth.guard';
import { ProfileComponent } from './profile/getProfile/profile.component';
const routes: Routes = [
{path:'',component:PostListComponent},
{path:'createPost', component:PostCreateComponent, canActivate: [AuthGuard]},
{path: "auth", loadChildren: "./auth/auth.module#AuthModule"},
{path:'profile/:username',component:ProfileComponent}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: [AuthGuard]
})
export class AppRoutingModule { }
I'm seeking assistance as I expected to be redirected successfully but faced challenges. Being relatively new to Angular, any guidance or support would be greatly appreciated. Thank you!