正在找時間邊學習Angular,之前主要都是後端為主,太笨所以在學習真的有障礙,尤其是關係到RxJS,目前是試著做登入動作,可是第一次點登入時都是Undefined…第二次點了之後才會傳回true。不知道是為什麼,找了一些討論也沒有方向,故來請教各位先進,希不吝教導。其它感覺比較不重要的就不貼上來,相關程式碼如下:(因之前找原因有亂試一些,雖註解起來但刪掉,所以有點亂抱歉!!~還有發文格式~~有待學習sorry)
login.component.ts
import { Component, OnInit, EventEmitter, Output } from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormControl } from "@angular/forms";
import { ActivatedRoute, Router } from '@angular/router';
import { AuthenticationService } from 'src/app/_services/authentication.service';
import { faCoffee } from '@fortawesome/free-solid-svg-icons';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
loginForm: FormGroup;
loading = false;
submitted = false;
returnUrl: string;
myIcon = faCoffee;
error: any;
constructor(
private formBuilder: FormBuilder,
private route: ActivatedRoute,
private router: Router
,private authenticationService: AuthenticationService ) {}
ngOnInit() {
this.loginForm = new FormGroup({
username: new FormControl('', Validators.required),
password: new FormControl('', Validators.required)
});
}
get f(): any { return this.loginForm.controls; }
onSubmit() {
this.submitted = true;
if (this.loginForm.invalid) {
return;
}
this.loading = true;
let result = this.authenticationService.login(this.f.username.value, this.f.password.value);
if (result === "true") {
this.router.navigate(['navigation']);
}
else {
console.log('登入失敗!');
this.loading = false;
}
}
}
authentication.service.ts:
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from "@angular/common/http";
import { APIBase } from "./apibase";
import { Router } from '@angular/router';
import { retry } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class AuthenticationService {
apiBase = new APIBase();
result: string = '';
loginResult: string = '';
constructor(private http: HttpClient, private router: Router, private alertservice: AlertService) { }
login(empno: number, emppassword: string) {
let url=this.apiBase.serverUrl + '/api/Hospital/getLoginResult';
const params = new HttpParams()
.set('empNo', empno.toString()).set('emppassword',emppassword);
// **trace時,第一次click submit這邊就空值,第二次再按時~就可以正常取到true....**
let result = this.http.get(url, {params})
.pipe(first())
.subscribe(
(res:Response) => {
this.loginResult = res.toString();
}
);
return loginResult;
}
logout() {
// remove user from local storage to log user out
localStorage.removeItem('currentUser');
}
}
目前環境版本參考如下:
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.8.6
@angular-devkit/build-angular 0.8.6
@angular-devkit/build-optimizer 0.8.6
@angular-devkit/build-webpack 0.8.6
@angular-devkit/core 0.8.6
@angular-devkit/schematics 0.8.6
@angular/cdk 7.0.3
@angular/cli 6.2.6
@angular/material 7.0.3
@ngtools/webpack 6.2.6
@schematics/angular 0.8.6
@schematics/update 0.8.6
rxjs 6.2.2
typescript 2.9.2
webpack 4.23.1