我是 Angular 4+ 的新手(目前使用 v.6)。我一直在尝试使用this.router.navigate(['/登陆'])从登录组件重定向到登陆组件的功能,它无法正常工作。
它将显示登录页面一秒钟,然后再次重定向回登录页面。
但是,例如,如果我尝试浏览普通链接,
<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: '**',
redirectTo: 'landing',
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})
试试这个,让我知道是否有帮助!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)