【发布时间】:2018-09-07 07:31:50
【问题描述】:
我尝试使用 angular5 实现登录功能。我有一个我不明白的错误。
这里是 login.component.ts 类:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { NgForm } from '@angular/forms';
import { FormGroup, FormControl } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';
import { HttpErrorResponse } from '@angular/common/http';
import { UserService } from '../auth/user.services';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
ngForm: NgForm;
login: string;
password: string;
myForm: FormGroup;
isLoginError : boolean = false;
connected : Boolean = false;
constructor(private userService: UserService, private router: Router) { }
ngOnInit() {
}
// Execute this when submit login form
OnSubmit(form){
console.log(form.value);
console.log(form.value.login);
this.connected = this.userService.authenticate(form.value.login,form.value.password).subscribe((data : any)=>{
return data.password = form.value.password;
//return true;
});
}
}
在 user.service.ts 我有验证方法:
authenticate(login:string, password:string) :Observable<Boolean> {
console.log('Authenticate ....');
const credentials = {login:login, password:password};
let headers = new HttpHeaders();
headers.append('Content-Type', 'application/json');
console.log('Appended content type ....');
console.log(' Login '+login+' Password '+password+' Headers '+headers);
//var data = this.http.get<any>(
// '/users?login?'+JSON.stringify(login),
// { headers }
//);
//console.log('Data '+data);
return this.http.get<any>(
'/users?login?'+JSON.stringify(login),
{ headers }
) //returns a User object having password
.map(user => user.password === password); // maps result to the desired true or false value
}
这是我的登录表单:
<div class="row">
<div class="col-md-6 offset-md-3 text-center">
<h2> Formulaire de connexion </h2>
<form (ngSubmit)="OnSubmit(loginForm)" #loginForm="ngForm">
<div class="form-group">
<label for="exampleInputEmail1">Login</label>
<input type="text" class="form-control" id="exampleInputEmail1" [(ngModel)]="login" name="login" aria-describedby="emailHelp" placeholder="Login">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" required [(ngModel)]="password" name="password" placeholder="Mot de passe">
</div>
<!-- <button type="submit" class="btn btn-primary" (click)="login()" [disabled]="!loginForm.form.valid">Connexion</button>
-->
<button type="submit" class="btn btn-primary" [disabled]="!loginForm.form.valid">Connexion</button>
</form>
</div>
</div>
我的代码编译,我可以访问登录页面。但是当我输入登录名和密码时,我有一个错误。而且我看不到对我的服务器的请求。我认为错误出现在我的 http 请求之前。这是我得到的错误:
TypeError:您在预期流的位置提供了“未定义”。您可以提供 Observable、Promise、Array 或 Iterable。堆栈跟踪:subscribeToResult@webpack-internal:///./node_modules/rxjs/_esm5/util/subscribeToResult.js:83:27 MergeMapSubscriber.prototype._innerSub@webpack-internal:///./node_modules/rxjs/_esm5/运营商/mergeMap.js:143:18 MergeMapSubscriber.prototype._tryNext@webpack-internal:///./node_modules/rxjs/_esm5/operators/mergeMap.js:140:9 MergeMapSubscriber.prototype._next@webpack-internal:/ //./node_modules/rxjs/_esm5/operators/mergeMap.js:123:13 Subscriber.prototype.next@webpack-internal:///./node_modules/rxjs/_esm5/Subscriber.js:97:13 ScalarObservable.prototype ._subscribe@webpack-internal:///./node_modules/rxjs/_esm5/observable/ScalarObservable.js:53:13 Observable.prototype._trySubscribe@webpack-internal:///./node_modules/rxjs/_esm5/Observable。 js:177:20 Observable.prototype.subscribe@webpack-internal:///./node_modules/rxjs/_esm5/Observable.js:165:88 MergeMapOperator.prototype.call@webpack-internal:///./node_modules/ rxjs/_esm5/operators/mergeMap.js:97:16 Observable.prototype.subscribe@webpa ck-internal:///./node_modules/rxjs/_esm5/Observable.js:162:13 FilterOperator.prototype.call@webpack-internal:///./node_modules/rxjs/_esm5/operators/filter.js:63 :16 Observable.prototype.subscribe@webpack-internal:///./node_modules/rxjs/_esm5/Observable.js:162:13 MapOperator.prototype.call@webpack-internal:///./node_modules/rxjs/_esm5 /operators/map.js:60:16 Observable.prototype.subscribe@webpack-internal:///./node_modules/rxjs/_esm5/Observable.js:162:13 MapOperator.prototype.call@webpack-internal:// /./node_modules/rxjs/_esm5/operators/map.js:60:16 Observable.prototype.subscribe@webpack-internal:///./node_modules/rxjs/_esm5/Observable.js:162:13 LoginComponent.prototype。 OnSubmit@webpack-internal:///./src/app/login/login.component.ts:33:9 View_LoginComponent_0/
错误在<form (ngSubmit)="OnSubmit(loginForm)" #loginForm="ngForm">行
我看不出问题出在哪里。我错过了什么?谢谢 谢谢
【问题讨论】:
-
是你的新
HttpClient的http实例吗? -
这是我的导入:import { HttpClient,HttpHeaders } from '@angular/common/http';
-
所以在您的 chrome 的网络调试器中,您没有看到请求发送到服务器?
-
我在调试器中看到没有请求发送到服务器
-
您找到解决方案了吗?
标签: angular typescript angular5