如何在我的服务中使用这个变量? (Angular 10)
How to use this variable in my service? (Angular 10)
我希望能够在我的身份验证服务中使用我的登录组件中的变量“selectedRole”。这是我的 login.component.ts 代码:
import { Component, ElementRef, Input, OnInit, ViewChild, NgModule } from '@angular/core';
import {NgForm} from '@angular/forms';
import { FormsModule } from '@angular/forms';
import { Router } from '@angular/router';
import {MatRadioModule} from '@angular/material/radio';
import { stringify } from 'querystring';
import {BrowserModule} from '@angular/platform-browser';
import { AuthenticationService } from "../authentication.service";
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
constructor(private router: Router, private authenticationService: AuthenticationService) { }
ngOnInit(): void {
}
selectedRole='User';
userID = '';
password = '';
RoleSelection(role){
this.selectedRole = role;
}
Login(){
if(this.selectedRole === 'User'){
this.router.navigate(['/userpage']);
}
else{
this.router.navigate(['/adminpage'])
}
}
}
这是我的认证服务:
import { Injectable } from '@angular/core';
import { SignInData } from 'src/SignInData';
@Injectable({
providedIn: 'root'
})
export class AuthenticationService {
private readonly mockedUser = new SignInData('user1', '123');
private readonly mockedAdmin = new SignInData('admin1', '456');
isAuthenticated = false;
constructor() { }
Authenticate(SignInData: SignInData): boolean {
if(this.CheckLoginDetails(SignInData)){
this.isAuthenticated = true;
return true;
}
this.isAuthenticated = false;
return false;
}
private CheckLoginDetails(SignInData: SignInData): boolean {
return this.CheckUserID(SignInData.getUserID()) && this.CheckPassword(SignInData.getPassword());
}
private CheckUserID(userID: string): boolean {
return userID === this.mockedAdmin.getUserID();
}
private CheckPassword(password: string): boolean {
return password === this.mockedAdmin.getUserID();
}
}
在 CheckUserID() 和 CheckPassword() 中我想实现一个条件,说:if selectedRole === 'User' or if selectedRole === 'Admin',但这需要我以某种方式从登录组件导入 selectedRole。我尝试使用 rxjs 但它没有用。
只需将 selectedRole 属性 添加到 AuthenticationService class 并从组件进行设置。
import { Injectable } from '@angular/core';
import { SignInData } from 'src/SignInData';
@Injectable({
providedIn: 'root'
})
export class AuthenticationService {
public selectedRole = 'User';
private readonly mockedUser = new SignInData('user1', '123');
private readonly mockedAdmin = new SignInData('admin1', '456');
isAuthenticated = false;
constructor() { }
Authenticate(SignInData: SignInData): boolean {
if (this.CheckLoginDetails(SignInData)) {
this.isAuthenticated = true;
return true;
}
this.isAuthenticated = false;
return false;
}
private CheckLoginDetails(SignInData: SignInData): boolean {
return this.CheckUserID(SignInData.getUserID()) && this.CheckPassword(SignInData.getPassword());
}
private CheckUserID(userID: string): boolean {
if (this.selectedRole === 'User') {
}
else if (this.selectedRole === 'Admin') {
}
return userID === this.mockedAdmin.getUserID();
}
private CheckPassword(password: string): boolean {
if (this.selectedRole === 'User') {
}
else if (this.selectedRole === 'Admin') {
}
return password === this.mockedAdmin.getUserID();
}
}
并从组件中设置它
RoleSelection(role){
this.authenticationService.selectedRole = role;
this.selectedRole = role;
}
我希望能够在我的身份验证服务中使用我的登录组件中的变量“selectedRole”。这是我的 login.component.ts 代码:
import { Component, ElementRef, Input, OnInit, ViewChild, NgModule } from '@angular/core';
import {NgForm} from '@angular/forms';
import { FormsModule } from '@angular/forms';
import { Router } from '@angular/router';
import {MatRadioModule} from '@angular/material/radio';
import { stringify } from 'querystring';
import {BrowserModule} from '@angular/platform-browser';
import { AuthenticationService } from "../authentication.service";
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
constructor(private router: Router, private authenticationService: AuthenticationService) { }
ngOnInit(): void {
}
selectedRole='User';
userID = '';
password = '';
RoleSelection(role){
this.selectedRole = role;
}
Login(){
if(this.selectedRole === 'User'){
this.router.navigate(['/userpage']);
}
else{
this.router.navigate(['/adminpage'])
}
}
}
这是我的认证服务:
import { Injectable } from '@angular/core';
import { SignInData } from 'src/SignInData';
@Injectable({
providedIn: 'root'
})
export class AuthenticationService {
private readonly mockedUser = new SignInData('user1', '123');
private readonly mockedAdmin = new SignInData('admin1', '456');
isAuthenticated = false;
constructor() { }
Authenticate(SignInData: SignInData): boolean {
if(this.CheckLoginDetails(SignInData)){
this.isAuthenticated = true;
return true;
}
this.isAuthenticated = false;
return false;
}
private CheckLoginDetails(SignInData: SignInData): boolean {
return this.CheckUserID(SignInData.getUserID()) && this.CheckPassword(SignInData.getPassword());
}
private CheckUserID(userID: string): boolean {
return userID === this.mockedAdmin.getUserID();
}
private CheckPassword(password: string): boolean {
return password === this.mockedAdmin.getUserID();
}
}
在 CheckUserID() 和 CheckPassword() 中我想实现一个条件,说:if selectedRole === 'User' or if selectedRole === 'Admin',但这需要我以某种方式从登录组件导入 selectedRole。我尝试使用 rxjs 但它没有用。
只需将 selectedRole 属性 添加到 AuthenticationService class 并从组件进行设置。
import { Injectable } from '@angular/core';
import { SignInData } from 'src/SignInData';
@Injectable({
providedIn: 'root'
})
export class AuthenticationService {
public selectedRole = 'User';
private readonly mockedUser = new SignInData('user1', '123');
private readonly mockedAdmin = new SignInData('admin1', '456');
isAuthenticated = false;
constructor() { }
Authenticate(SignInData: SignInData): boolean {
if (this.CheckLoginDetails(SignInData)) {
this.isAuthenticated = true;
return true;
}
this.isAuthenticated = false;
return false;
}
private CheckLoginDetails(SignInData: SignInData): boolean {
return this.CheckUserID(SignInData.getUserID()) && this.CheckPassword(SignInData.getPassword());
}
private CheckUserID(userID: string): boolean {
if (this.selectedRole === 'User') {
}
else if (this.selectedRole === 'Admin') {
}
return userID === this.mockedAdmin.getUserID();
}
private CheckPassword(password: string): boolean {
if (this.selectedRole === 'User') {
}
else if (this.selectedRole === 'Admin') {
}
return password === this.mockedAdmin.getUserID();
}
}
并从组件中设置它
RoleSelection(role){
this.authenticationService.selectedRole = role;
this.selectedRole = role;
}