gpt4 book ai didi

javascript - 比较 Subscribe Angular 和 Observable RXJS 中的数据

转载 作者:行者123 更新时间:2023-12-03 01:49:51 26 4
gpt4 key购买 nike

我是 Angular 新手,并使用 in-memory-web-api 编写了以下代码用于登录 POC,

数据库服务.服务:

import { InMemoryDbService } from 'angular-in-memory-web-api';

export class InMemoryDataService implements InMemoryDbService {
createDb() {
let users = [
{id: 1, username: 'user1', password:'user1', name:'John'},
{id: 2, username: 'user2', password:'user2', name:'David'},
{id: 3, username: 'user3', password:'user3', name:'Brad'},
{id: 4, username: 'user4', password:'user4', name:'Jim'},
{id: 5, username: 'user5', password:'user5', name:'Saun'}
];

return {users};
}
}

用户.服务:

import { loginUser } from '../_model/user';

@Injectable()

export class UserService {

private headers = new Headers({ 'Content-Type': 'application/json' });
private userUrl = 'api/users'; // URL to web api

constructor(private http: Http) {
}


getLogin(username: string, password: string) {
const url = `${this.userUrl}`;
return this.http.get(url).map(res => res.json());
}
}

模型/user.ts

export class loginUser {
constructor(
public id: number,
public username: string,
public password: string,
public name: string) { }
}

登录.组件.ts

login(form: FormGroup) {

if (this.loginForm.valid) {
this.userData.getLogin(form.value.loginUserTxt, form.value.loginPassTxt)
.subscribe(
data => {
console.log(data);
//comparing the data from the array in DB-service.service
},
error => {
console.log("Fail");
});

}

我的问题是如何将 login.component.ts 订阅中的数据与输入值进行比较。

最佳答案

我已经更新了订阅中的以下代码

this.loggedUser = this.userData.getAuthenticate(data, form.value.loginUserTxt, form.value.loginPassTxt)    
if (this.loggedUser == "Invalid") {
//error
} else {
//redirect
}

谢谢大家

关于javascript - 比较 Subscribe Angular 和 Observable RXJS 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50465899/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com