Angular 角度7:如何从类中获取数据
学生班级: //我这里有一个学生班(mock students.ts) 以上是学生班(mock Student.ts) 以上是我的登录组件。我的问题是如何从Angular 角度7:如何从类中获取数据,angular,angular7,Angular,Angular7,学生班级: //我这里有一个学生班(mock students.ts) 以上是学生班(mock Student.ts) 以上是我的登录组件。我的问题是如何从学生班级获取数据,并使数据可以登录到下一条路线?谢谢,我希望你们都能理解 login.component.html <div fxLayout="column" fxLayoutAlign="space-around center"> <mat-card class="login-card"> <
学生
班级获取数据,并使数据可以登录到下一条路线?谢谢,我希望你们都能理解
login.component.html
<div fxLayout="column" fxLayoutAlign="space-around center">
<mat-card class="login-card">
<mat-card-header>
<mat-card-title>Login</mat-card-title>
</mat-card-header>
<mat-card-content>
<form class="login-form">
<mat-form-field class="login-full-width">
<input matInput placeholder="Matric Card Number" [(ngModel)]="id" name="id" required/>
</mat-form-field>
<mat-form-field class="login-full-width">
<input matInput placeholder="Password" [(ngModel)]="password" type="password" name="password" required/>
</mat-form-field>
</form>
</mat-card-content>
<mat-card-actions align="end">
<button mat-raised-button (click)="login()" color="primary">Login</button>
<button mat-raised-button color="primary">Reset</button>
</mat-card-actions>
</mat-card>
</div>
从ngOnInit中删除代码,并在login方法中调用服务
login():void{
this.studentService.getStudent()
.订阅(学生=>{
const result=student.filter(s=>this.id==s.id&&this.password==s.password);
如果(result.length>0){
this.router.navigate([“user”]);
返回;
}
警报(“无效”);
});
}
login.component.ts
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { Student } from './student';
import { STUDENTS } from './mock-students';
@Injectable({
providedIn: 'root'
})
export class StudentService {
constructor() { }
getStudents(): Observable<Student[]> {
return of(STUDENTS);
}
}
import { Student } from './student';
export const STUDENTS: Student[] = [
{ id: 'AM1705002120', password: 'hisyam' },
{ id: 'AM1705002121', password: 'taca' },
{ id: 'AM1705002122', password: 'deena' },
{ id: 'AM1705002123', password: 'ucop' },
{ id: 'AM1705002124', password: 'ha' },
]
从'@angular/core'导入{Component,OnInit};
从'@angular/Router'导入{Router};
从“@angular/common”导入{Location};
从“../Student”导入{Student};
从“../student.service”导入{StudentService};
@组成部分({
选择器:“应用程序登录”,
templateUrl:'./login.component.html',
样式URL:['./login.component.css']
})
导出类LoginComponent实现OnInit{
学生:学生;
建造师(
私人学生服务:学生服务,
专用路由器:路由器,
私人地点:地点
) { }
id:字符串;
密码:字符串;
恩戈尼尼特(){
}
login():void{
this.studentService.getStudent(this.id,this.password)
.订阅(学生=>{
this.router.navigate([“user”]);
},err=>alert(err));
}
}
您的意思是要将Student对象传递到下一个组件,以便在登录后路由用户?是的,先生,我已经编辑了代码,以便您可以查看更多信息。您可以在路由中传递数据或将其存储到会话存储中。无论如何,我认为您的问题是oke的一个副本,如何根据Student对象更改if语句使其为真?它说“[ts]无法调用类型缺少调用签名的表达式。类型“StudentService”没有完整的调用签名[2349]。是的,最后一个建议是重构代码。它不存在。但是你最终应该以这种方式实现getStudent。你能帮我检查StudentService吗,我做得对吗?完成更新,包括student.service.ts和mock-student.tsStudent.service.ts``从'@angular/core'导入{Injectable};导入{Observable,of}来自'rxjs';import{Student}来自'/Student';import{Student}来自'/mock STUDENTS';@Injectable({providedIn:'root'})导出类StudentService{constructor(){}getStudents():Observable{return of(STUDENTS);}```
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { Student } from './student';
import { STUDENTS } from './mock-students';
@Injectable({
providedIn: 'root'
})
export class StudentService {
constructor() { }
getStudents(): Observable<Student[]> {
return of(STUDENTS);
}
}
import { Student } from './student';
export const STUDENTS: Student[] = [
{ id: 'AM1705002120', password: 'hisyam' },
{ id: 'AM1705002121', password: 'taca' },
{ id: 'AM1705002122', password: 'deena' },
{ id: 'AM1705002123', password: 'ucop' },
{ id: 'AM1705002124', password: 'ha' },
]