【发布时间】:2019-09-03 00:18:24
【问题描述】:
我已经创建了一个 Angular 应用程序,并且在我的一项服务中,在我调用 http.post 方法并订阅它之后,我想重定向到我之前的同一页面,但该页面有一个参数,例如。 "http://localhost:3000/profile/aRandomName"
我尝试调用路由器方法,但它不起作用。
这是我服务的一部分
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]);
})
}
这是我的 app-routing.module.ts
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 { }
我预计会被重定向,你能帮我解决这个问题吗,我是 Angular 新手。谢谢!
【问题讨论】:
标签: angular typescript