首页 > 解决方案 > 导航到组件路由时出错

问题描述

我正在使用带有打字稿的 Angular 6,并尝试使用导航从一个组件路由(RegisterComponent)导航到另一个组件路由(ProfileComponent)(使用 routerLink 也不起作用)。我尝试了很多我发现的例子,但我一直遇到同样的错误:

错误错误:未捕获(承诺中):TypeError:无法读取未定义的属性>'removeEventListener' TypeError:无法读取未定义的属性'removeEventListener'

我的代码:

app.module.ts

import { RouterModule, Routes } from '@angular/router';
//hidden imports ...

export const appRoutes: Routes = [

       {path: '', component: RegisterComponent}, 
       {path: 'profile', component: ProfileComponent}, 


]

@NgModule({
  declarations: [
    AppComponent,
    RegisterComponent,
    ProfileComponent,
  ],
  imports: [
    BrowserModule,
    RouterModule.forRoot(appRoutes),
    HttpClientModule,
    FormsModule,
    ReactiveFormsModule,
    CustomFormsModule,
    BrowserAnimationsModule,
  ],
  providers: [],
  bootstrap: [AppComponent],
  exports: [ RouterModule]
})
export class AppModule { }

app.component.html

<router-outlet></router-outlet>

注册组件.ts

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

export class RegisterComponent {
constructor(private router: Router, private route: ActivatedRoute) { }

test(){
    this.router.navigate(["../profile"], {relativeTo: this.route});
    //this.router.navigate(["/profile"], {relativeTo: this.route});
  }
}

register.component.html

<div (click)="test()">click</div>

标签: angulartypescript

解决方案


推荐阅读