【问题标题】:How can I make the App Routing work in this case?在这种情况下,如何使 App Routing 工作?
【发布时间】: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


    【解决方案1】:

    你必须像这样传递路由参数:

    this.router.navigate(['profile/', thisUserUsername]);
    

    这应该可以修复导航错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-25
      • 1970-01-01
      • 2018-06-16
      • 2017-06-21
      • 2018-01-19
      • 2021-12-06
      • 2020-12-27
      相关资源
      最近更新 更多