Ich benutze angular2 mit Typoskript. In einem der Service Ich gründe und bekommen den Wert einer Variablen wie folgt aus:Variable in Typoskript
import {Injectable} from '@angular/core';
@Injectable()
export class UserProfileService {
isLoggedIn: boolean;
constructor() {
}
setLoggedInStatus(status) {
console.log('status: ', status);
this.isLoggedIn = status;
console.log('this.isLoggedIn : ', this.isLoggedIn) //setting to true or false here
}
getLoggedInStatus() {
return this.isLoggedIn;
}
}
aber wenn ich versuche, den Wert mit getLoggedInStatus() zu bekommen, erhalte ich jedes Mal nicht definiert. Warum passiert das?
Einstellung hier
import {Component} from '@angular/core';
import {FormControl, FormGroup, Validators} from '@angular/forms';
import {SharedService} from '../../shared.service';
import {UserProfileService} from '../../authComponent/login/user-profile.service';
@Component({
selector: 'login',
templateUrl: 'app/components/authComponent/login/login.component.html',
providers: [SharedService, UserProfileService]
})
export class LoginComponent implements OnInit {
matched: boolean = false;
constructor(private sharedService: SharedService, private userService: UserProfileService) {
}
ngOnInit() {
this.myForm = new FormGroup({
email: new FormControl('', [Validators.required]),
password: new FormControl('', [Validators.required])
})
}
submit({value, valid}) {
if (!valid) {
return;
}
else {
this.sharedService.getData('users')
.subscribe(result => {
console.log('result: ', result, 'value passed: ', value)
result.forEach((record) => {
if ((record.email == value.email && record.password == value.password) && !this.matched) {
this.matched = true;
}
else {
this.matched = false;
}
});
if (!this.matched)
console.log('wrong credentials entered');
this.userService.setLoggedInStatus(this.matched);
})
}
}
}
hier bekommen
import {Injectable} from '@angular/core';
import {
CanActivate,
CanActivateChild,
Route,
Router,
ActivatedRouteSnapshot,
RouterStateSnapshot
} from '@angular/router';
import {UserProfileService} from './authComponent/login/user-profile.service';
@Injectable()
export class CanActivateAuthGuard implements CanActivate, CanActivateChild {
constructor(private userProfileService: UserProfileService, private router: Router) {
}
canActivateChild(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
return this.canActivate(next, state);
}
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
console.log('this.userProfileService.getLoggedInStatus: ', this.userProfileService.getLoggedInStatus())
if (this.userProfileService.getLoggedInStatus()) {
return true;
}
this.router.navigate(['/login'], {queryParams: {redirectTo: state.url}});
return false;
}
}
Sie verwenden 'setLoggedInStatus' in einer Komponente und' getLoggedInStatus' in einer anderen Komponente, richtig? –
Wie Sie einstellen und bekommen? – Sajeetharan
Sie rufen es wahrscheinlich für eine andere Instanz des Dienstes an. Unmöglich zu sagen, warum, wenn Sie kein Beispiel postulieren, das das Problem reproduziert. –