"ActivatedRoute"类型中不存在"导航"属性

Fig*_*Fig 7 url-routing angular-ui-router angular

我正试图通过配置文件组件(前一个组件)导航到listProfiles组件.我正在使用ActivatedRoute并试图通过导航到它this.router.navigate(['/listProfiles)

组件中应该导航到listProfile组件的代码

import {ActivatedRoute, Router} from '@angular/router';

constructor(private router: ActivatedRoute){}

deleteProfile():void{
   this.router.navigate(['/listProfiles']); //Gives the error message in the title
}
Run Code Online (Sandbox Code Playgroud)

app.module.ts

import { ListProfilesComponent } from './list-profiles/list-profiles.component';
import { ProfileComponent } from './profile/profile.component';

const appRoutes: Routes = [
{ path: 'addProfile', component: AddProfileComponent },
{ path: 'listProfiles', component: ListProfilesComponent},
{ path: 'profile/:id', component: ProfileComponent},
{ path: 'login', component: LoginComponent}
];

@NgModule({
  declarations: [
  AppComponent,
  ListProfilesComponent,
  ProfileComponent,
  ],
  imports: [
  FormsModule,
  ReactiveFormsModule,
  NoopAnimationsModule,
  BrowserModule,
  HttpModule,
  RouterModule.forRoot(
    appRoutes,
    {enableTracing: true}
    )
  ],
  providers: [ StorageService, LoginService, ClientIDService],
  bootstrap: [AppComponent]
})
Run Code Online (Sandbox Code Playgroud)

Naw*_*rez 14

你必须添加:

constructor(private route:ActivatedRoute,private router:Router) { }
Run Code Online (Sandbox Code Playgroud)

然后 :

 this.router.navigate(...
Run Code Online (Sandbox Code Playgroud)

  • 因为除非发布时间超过15分钟,否则我无法这样做。正如我所说,我将在几分钟后将其作为答案提交 (2认同)