Студент класс:
// Я получил класс ученика здесь (mock-student.ts)
export class Student {
id: string;
password: string;
}
Выше класс ученика (mock-student.ts)
import { Component, OnInit } from '@angular/core';
import {Router} from '@angular/router';
import { Student } from '../student';
import { STUDENTS } from '../mock-students';
import { StudentService } from '../student.service';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
constructor(private studentService: StudentService, private router: Router) { }
student: Student[];
id: string;
password: string;
ngOnInit() {
this.getStudent();
}
login(): void {
// I want to make a change here
if(this.id == '123' && this.password == '123') {
this.router.navigate(["user"]);
} else {
alert("Invalid")
}
}
getStudent(): void {
this.studentService.getStudent()
.subscribe(student => this.student = student);
}
}
Выше мой LoginComponent.У меня вопрос: как я могу получить данные из класса Student
и сделать так, чтобы данные могли войти на следующий маршрут?Спасибо, я надеюсь, вы все понимаете
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>
Student.service.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);
}
}
mock-student.ts
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' },
]