Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 是否在HTML视图中显示sesstionStorage字符串?_Angular_Session_Model View Controller - Fatal编程技术网

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');
   }
}

我不确定我是否理解为什么这需要成为一个问题。您已经知道如何从会话存储中获取值,那么为什么在获取值时不能存储该值呢?