Angular 是否在HTML视图中显示sesstionStorage字符串?
我只想在角度视图中显示存储在会话存储中的数据 我的遗产存储:Angular 是否在HTML视图中显示sesstionStorage字符串?,angular,session,model-view-controller,Angular,Session,Model View Controller,我只想在角度视图中显示存储在会话存储中的数据 我的遗产存储: sessionStorage.getItem('username'); My dashboard.ts文件: export class DashboardComponent implements OnInit { currentUserSubscription: Subscription; constructor(private authenticationService: AuthenticationService){
sessionStorage.getItem('username');
My dashboard.ts文件:
export class DashboardComponent implements OnInit {
currentUserSubscription: Subscription;
constructor(private authenticationService: AuthenticationService){
}
ngOnInit() {
}
}
export class DashboardComponent implements OnInit {
username: string;
constructor(private authenticationService: AuthenticationService){}
ngOnInit() {
this.username = this.authenticationService.getUsername();
}
}
我的HTML文件:
<div *ngIf = "username">
<p class="introText"><b>Welkom:</b> {{ username }}
<button mat-button (click)="clockIn()" class="mat-raised-button mat-primary clockButton">
Inklokken
</button>
<button mat-button type="submit" class="mat-raised-button mat-basic">
Uitklokken
</button> <br>
<b>Ingelogd: </b>{{dateTime}}
</div>
Welkom:{{username}
因克洛肯
尤特克洛肯
Ingelogd:{{dateTime}
及
Welkom:{{username}
dashboard.ts文件:
export class DashboardComponent implements OnInit {
currentUserSubscription: Subscription;
constructor(private authenticationService: AuthenticationService){
}
ngOnInit() {
}
}
export class DashboardComponent implements OnInit {
username: string;
constructor(private authenticationService: AuthenticationService){}
ngOnInit() {
this.username = this.authenticationService.getUsername();
}
}
authentication.service.ts
@Injectable({
providedIn: 'root'
})
export class AuthenticationService{
getUsername():string {
return sessionStorage.getItem('username');
}
}
我不确定我是否理解为什么这需要成为一个问题。您已经知道如何从会话存储中获取值,那么为什么在获取值时不能存储该值呢?