我是Angular 4的新手(目前正在使用v.6).我一直在尝试使用this.router.navigate([‘/ landing’])函数从登录组件重定向到登陆组件,它无法正常工作.
它将显示登录页面一秒钟,然后再次重定向回登录页面.
它将显示登录页面一秒钟,然后再次重定向回登录页面.
但是,如果我尝试浏览正常链接,例如,
<a routerLink="/landing">landing</a>
它工作正常.
这是我的app-routing.module.ts
import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { Routes,RouterModule } from '@angular/router'; const routes: Routes = [ { path: 'login',loadChildren: './login/login.module#LoginModule' },{ path: 'landing',loadChildren: './landing/landing.module#LandingModule' },{ path: '',redirectTo: 'landing',pathMatch: 'full' },{ path: '**',pathMatch: 'full' } ]; @NgModule({ imports: [ RouterModule.forRoot(routes) ],exports: [ RouterModule ] }) export class AppRoutingModule { }
export class LoginComponent implements OnInit { constructor(private router: Router) { } ngOnInit() { } login(){ this.router.navigate(['/landing']); } }
这是landing-routing.module.ts中的代码
import { NgModule } from '@angular/core'; import { Routes,RouterModule } from '@angular/router'; import { LandingComponent } from './landing.component'; const routes: Routes = [ { path:'',component: LandingComponent } ]; @NgModule({ imports: [RouterModule.forChild(routes)],exports: [RouterModule] }) export class LandingRoutingModule { }
谢谢你的帮助.
解决方法
我遇到了类似的问题,我是如何解决它的:
在Component.ts中
constructor(private router: Router,private route: ActivatedRoute){} this.router.navigate(['../landing'],{relativeTo: this.route})
试试这个,让我知道它是否有帮助!!!