[英]Angular router.navigate not redirecting to target page
我有一個登錄表單,該表單應該重定向到主應用程序頁面:
我不確定如何導航至以下頁面:app / analytics,並認為這是由於我的路線設置導致提交登錄表單后無法導航至正確的頁面。
到目前為止,這是我嘗試過的。
應用路線
export const ROUTES: Routes = [
{
path: 'login', loadChildren: './pages/login/login.module#LoginModule'
},
{
path: '', redirectTo: 'app/analytics', pathMatch: 'full'
},
{
path: 'app', loadChildren: './layout/layout.module#LayoutModule', canActivate: [AuthGuard]
},
{
path: 'error', component: ErrorComponent
},
{
path: '**', component: ErrorComponent
}
];
布局路線
const routes: Routes = [
{ path: '', component: LayoutComponent, children: [
{ path: 'analytics', loadChildren: '../pages/analytics/analytics.module#AnalyticsModule' },
{ path: 'profile', loadChildren: '../pages/profile/profile.module#ProfileModule' },
{ path: 'widgets', loadChildren: '../pages/widgets/widgets.module#WidgetsModule' },
]}
];
export const ROUTES = RouterModule.forChild(routes);
我的路由器插座存在於layout.template和app.component中
我有一個登錄組件 ,該調用
this.router.navigate ['app / analytics']
export class LoginComponent {
@HostBinding('class') classes = 'login-page app';
constructor(private auth: AuthService, private router: Router) {}
loginUser(event) {
event.preventDefault();
const target = event.target;
console.log(target)
const username = target.querySelector('#username').value;
const password = target.querySelector('#password').value;
this.auth.getUserDetails(username, password).subscribe(data => {
console.log(data.status)
if(data.status == "success") {
this.router.navigate['app/analytics'];
this.auth.setLoggedIn(true);
console.log(this.auth.isLoggedIn);
}
else {
window.alert(data.message)
}
});
}
}
這被稱為我的login.template :
<form class="login-form mt-lg" (submit)="loginUser($event)">
<div class="form-group">
<input type="text" class="form-control" id="username" value="USERNAME" placeholder="Username">
</div>
<div class="form-group">
<input class="form-control" id="password" type="password" value="PASSWORD" placeholder="Password">
</div>
<div class="clearfix">
<div class="btn-toolbar float-right m-t-1">
<button type="button" class="btn btn-default btn-sm">Create an Account</button>
<button type="submit" class="btn btn-inverse btn-sm" >Login</button>
</div>
</div>
<div class="row m-t-1">
<div class="col-md-6 order-md-2">
<div class="clearfix">
<div class="form-check abc-checkbox widget-login-info float-right pl-0">
<input class="form-check-input" type="checkbox" id="checkbox1" value="1">
<label class="form-check-label" for="checkbox1">Keep me signed in </label>
</div>
</div>
</div>
<div class="col-md-6 order-md-1">
<a class="mr-n-lg" href="#">Trouble with account?</a>
</div>
</div>
</form>
要解決您的問題,您必須編輯router.navigate
中的LoginComponent
以從根目錄開始: this.router.navigate(['/app/analytics']);
(不要忘記括號),否則路由器將嘗試導航到login/app/analytics
。
另外,請確保您的guard
讓您通過。
我也想從你移除重定向app.routes
{path: '', redirectTo: 'app/analytics', pathMatch: 'full'},
並把它里面layout.routes
像: {path: '', redirectTo: './analytics', pathMatch: 'full'},
請嘗試以此順序調用您的方法
this.auth.getUserDetails(username, password).subscribe(data => {
console.log(data.status)
if(data.status == "success") {
this.auth.setLoggedIn(true); // First set login to true
this.router.navigate(['/app/analytics']); // Then navigate to the route
console.log(this.auth.isLoggedIn);
}
else {
window.alert(data.message)
}
});
您必須使用以下方法給組件人:
this.router.navigate [''];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.