TypeError:无法读取未定义的属性“email”

时间:2017-03-18 15:56:50

标签: angular

我有登录时出现的日志组件:

public class StudentThread extends Thread {

    public void run(){
       Student s[] = new Student[10];


        s[0] = new MathStudent("Smith");
        s[1] = new MathStudent("Jack");
        s[2] = new MathStudent("Victor");
        s[3] = new MathStudent("Mike");
        s[4] = new ScienceStudent("Dave");
        s[5] = new ScienceStudent("Oscar");
        s[6] = new ScienceStudent("Peter");
        s[7] = new ComputerStudent("Philip");
        s[8] = new ComputerStudent("Shaun");
        s[9] = new ComputerStudent("Scott");


        for (int j = 0; j < 10; j++) {
           for (Student item : s) {
               System.out.print(item.getSubjects() + " - " + "Count:");
               item.getCounter();
           }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
               // TODO Auto-generated catch block

            }
        }

    }

}

在路线中我添加哈希。import {Component, EventEmitter, Input, OnChanges,OnInit} from '@angular/core'; import {Observable} from 'rxjs/Rx'; import {Router} from '@angular/router'; import { LogService } from '../services/logService.service'; import {User,SharedService} from '../../SharedService' @Component({ selector: 'dashboard', providers: [LogService], template: ` <div class="loader" *ngIf="loader"> <img src="../../images/loader-img.gif" alt="" /> </div> <div class="container"> <div class="row"> <div class="col-md-10 col-md-offset-1"> <div class="panel panel-default"> <div class="panel-heading">Dashboard</div> <div class="panel-body"> You are logged in! Welcome <span>{{ user.name }} </span> <button type="submit" class="logout btn btn-primary" (click)="logout()">Logout</button> <hr/> <table class="table"> <thead> <tr> <th>Checkin time</th> <th>Checkout time</th> <th>Total time</th> </tr> </thead> <tbody> <tr *ngFor="let log of logs"> <td>{{log.checkin}} </td> <td>{{log.checkout}}</td> <td>{{log.total_time}}</td> </tr> </tbody> </table> </div> <button *ngIf="checking" type="submit" class="btn btn-primary btn-block" (click)="checkout(user.email)">Checkout</button> <button *ngIf="!checking" type="submit" class="btn btn-warning btn-block" (click)="checkin(user.email)">Checkin</button> </div> </div> </div> </div> ` }) export class LogsComponent implements OnInit, OnChanges { private checking = false; logs: any; log_id:any; user:User; loader = false; constructor( private logService: LogService, private router: Router, private ss:SharedService ){ this.user=ss.getUserDetail(); } checkin(values){ this.loader = true; var current = this; // Variable to hold a reference of addComment/updateComment let logOperation:Observable<any>; logOperation = this.logService.CheckIn(values); logOperation.subscribe( res => { this.checking = true; this.log_id = res.log_id; this.loader = false; }, err => { this.loader= false;} ); } checkout(values){ this.loader = true; var current = this; // Variable to hold a reference of addComment/updateComment let logOperation:Observable<any>; logOperation = this.logService.CheckOut(values,this.log_id); logOperation.subscribe( res => { this.checking = false; this.loader = false; }, err => { this.loader = false; } ); this.loadLogs(); } logout(){ this.loader = true; var current = this; this.logService.logout() .subscribe( res => { if(res.isLoggedIn == false){ this.loader = false; current.router.navigate(['/']); } }, err => { this.loader = false;} ); } loadLogs(){ this.loader = true; // Get all comments this.logService.getLogs() .subscribe( res => { this.logs =res.logs; this.loader = false; }, err => { this.loader = false; } ); } ngOnInit(){ // Load comments this.loadLogs() } ngOnChanges(changes:any) { // Listen to the 'list'emitted event so as populate the model // with the event payload } }

当我登录时,我有这条路线:

useHash: true

但是当我刷新时,我从标题中得到错误。有什么建议为什么?我需要以某种方式发送数据吗?

2 个答案:

答案 0 :(得分:1)

当您刷新页面时,不会调用构造函数(因为组件已经构建),您应该避免在构造函数中初始化值。请改用ngOnInit,因为每次创建角色时都会调用它。

constructor(
     private logService: LogService,
     private router: Router,
     private ss:SharedService
     ){ }

ngOnInit() {
  this.user=ss.getUserDetail()
}

答案 1 :(得分:1)

使用ngOnInit而不是constructor.ngOnInit()是更好的地方&#34; start&#34;

ngOnInit() {
  this.user=ss.getUserDetail()
}

在使用之前,还要添加一个安全的操作符来检查是否有值,

<button *ngIf="checking" type="submit" class="btn btn-primary btn-block" (click)="checkout(user?.email)">Checkout</button>

<强>更新 刷新时,共享服务中的用户变量变为未定义。 当你第一次拿到它时,尝试将它存储在本地存储中。

setUserDetail(res){ 
//this.user=res; 
localStorage.setItem('login',JSON.stringify(res)) 
}
getUserDetail(){ 
return JSON.parse(localStorage.getItem('login')); 
}