【发布时间】:2020-01-11 04:21:28
【问题描述】:
我有一个登录表单,应该重定向到主应用程序页面:
我不确定如何导航到此页面:app/analytics,并认为这是由于我的路由设置导致我在提交登录表单后无法导航到正确的页面。
这是我迄今为止所尝试的。
app.routes
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
}
];
layout.routes
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>
【问题讨论】:
-
你应该使用
this.router.navigate['/app/analytics'];。 app 之前的斜线很重要。请参阅this answer 以获取更多信息。 -
我认为您需要将其称为
this.router.navigate(['app', 'analytics']);。 -
你在 analytics.module 中使用 RouterModule 吗?
-
@yusufcmrt 我确实在 analytics.module 中使用 routermodule,RouterModule.forChild(routes),