【发布时间】:2018-05-23 22:00:23
【问题描述】:
这可能是一个非常常见的angular 路由问题,但我似乎无法让它工作。我不确定即使在阅读了 angular 文档之后我是否清楚地理解了所有概念。以下是我想要实现的以及我尝试过的:
我有一个登录应用程序。当用户在登录表单中单击继续时,我希望应用程序重定向到新页面/应用程序。为此:
- 我首先尝试在
app.module.ts文件中的@NgModule imports列表中添加RouterModule。
import { RouterModule, Routes } from '@angular/router';
// Routes
const routes: Routes = [
{path: "", component: InvestigatorLoginComponent},
{path: 'invdashboard', component: InvDashboardComponent}
];
//...
@NgModule({
// ...
imports: [
// ...
RouterModule.forRoot(
routes, { enableTracing: true} // <= debugging
),
// ...
})
export class AppModule {}
- 将
<router-outlet></router-outlet>添加到<inv-dashboard>组件:
app.component.html
<div id='login-app-wrapper'>
<nav-top></nav-top>
<investigator-login></investigator-login>
</div>
inv-dashboard.component.html
<router-outlet></router-outlet>
<p>
inv-dashboard works!
</p>
inv-dashboard.component.ts
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'inv-dashboard',
templateUrl: './inv-dashboard.component.html',
styleUrls: ['./inv-dashboard.component.css']
})
export class InvDashboardComponent implements OnInit { // ... }
-
登录组件:
investigator-login.component.html<form class='inv-login' [formGroup]="invLoginForm" (ngSubmit)="onSubmit(invLoginForm.value)"> <mat-form-field class='inv-login-full-width-input'> <input matInput placeholder="Username" formControlName="fcUsername" value="" maxlength="30"> </mat-form-field> <mat-form-field class='inv-login-full-width-input'> <input matInput placeholder="Password" formControlName="fcPassword" value="" maxlength="25"> </mat-form-field> <div class='prcd-btn-container'> <button mat-button color='primary' type='submit'>Proceed</button> </div> </form>
investigator-login.component.ts
`onSubmit(f: any): void {
this.authenticator.postContent(f)
.subscribe(
this.handleLoginResponse,
err => { throw new Error('failed'); }
);
}
handleLoginResponse(res: IResponseSuccess) {
if(res.status === 200) {
// success
console.log(res.template);
}
else {
console.error('Server error ' + res.status);
}
}`
填写表格后,我点击继续,没有任何反应。堆栈轨道显示没有重定向。我是否需要向表单中的proceed 按钮处理程序添加任何重定向代码?我真的不确定还要添加什么。
谢谢
【问题讨论】:
-
你是如何重定向的?请分享您的登录组件代码
-
@YousefKhan 用
login.component更新了问题