Я разрабатываю страницу входа. При успешном входе в систему пользователь будет перенаправлен на другую страницу. В форме я сохранил форму attribute method = "post", потому что я не хочу, чтобы имя пользователя и пароль отображались на URL-адресе. Однако при успешном входе в систему, когда выполняется this.router.navigate(['post']);
, я получаю сообщение об ошибке «не может POST/post».Angular 2 перейти на другую страницу при входе
Если я пишу метод = «получить», все работает, но имя пользователя и пароль появляются на URL-адресе. Пожалуйста, дайте мне знать, что было бы правильным подходом справиться с этим:
login.component.ts
import { Component, trigger, state, style, transition, animate } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { AuthenticatorService } from './authenticator.service';
import { Router } from '@angular/router';
@Component({
selector: 'login',
templateUrl: 'template/login.html',
animations: [
trigger('heroState', [
state('inactive', style({
height: '0',
paddingTop: '0',
paddingBottom: '0',
marginTop: '0',
marginBottom: '0',
visibility: 'hidden',
overflowY: 'hidden'
})),
state('active', style({
})),
transition('inactive => active', animate('300ms ease-in')),
transition('active => inactive', animate('300ms ease-out'))
])
]
})
export class LoginComponent {
private formGroup: FormGroup;
private authenticated = false;
private loginErrorMessage = 'inactive';
constructor(formBuilder: FormBuilder, private authenticator: AuthenticatorService, private router: Router) {
this.formGroup = formBuilder.group({
username: ['', [Validators.required, Validators.minLength(3)]],
password: ['', Validators.required]
});
}
onClick() {
this.authenticated = this.authenticator.authenticate(this.formGroup.controls["username"].value, this.formGroup.controls["password"].value);
this.loginErrorMessage = this.authenticated?'inactive':'active';
setTimeout(()=>{
if(this.loginErrorMessage === 'active') {
this.loginErrorMessage='inactive';
}
},3000);
if(this.authenticated) {
this.router.navigate(['post']);
}
}
getLoginStatus() {
return this.loginErrorMessage;
}
}
образуют часть login.html
<form [formGroup]="formGroup" role="form" action="" method="post" class="login-form">
<div *ngIf="formGroup.controls.username.touched && formGroup.controls.username.errors && formGroup.controls.username.errors.required" class="error-text">Username is required</div>
<div *ngIf="formGroup.controls.username.touched && formGroup.controls.username.errors && formGroup.controls.username.errors.minlength" class="error-text">Username must be atlease three characters long</div>
<div class="form-group">
<label class="sr-only" for="form-username">Username</label>
<input formControlName="username" type="text" name="form-username" placeholder="Username..." class="form-username form-control" id="form-username">
</div>
<div class="form-group">
<label class="sr-only" for="form-password">Password</label>
<input formControlName="password" type="password" name="form-password" placeholder="Password..." class="form-password form-control" id="form-password">
</div>
<button [disabled]="!formGroup.valid" (click)="onClick()" type="submit" class="btn">Sign in!</button>
</form>
Возможного дубликатом [Как отправить форму на сервер в Angular2 ] (http://stackoverflow.com/questions/34835516/how-to-submit-form-to-server-in-angular2) – echonax