简体   繁体   English

为什么我无法访问ngOnInit上设置的对象

[英]Why am I unable to get access to object that was set on ngOnInit

Ran into a few problems lately and wonder why I can't access an object, that was set in the ngOnInit function for later purposes, like in another function? 最近遇到了一些问题,想知道为什么我不能访问在ngOnInit函数中设置用于稍后目的的对象,就像在另一个函数中一样?

I want to access and use this.appointmentDetailId in my cancelAppointment() function but it is undefined . 我想在我的cancelAppointment()函数中访问和使用this.appointmentDetailId ,但它是undefined Hope someone can help. 希望有人能帮忙。 Thanks. 谢谢。

Here's my code: 这是我的代码:

export class AppointmentDetailComponent implements OnInit {
  id: any;
  appointmentDetailId: any;
  appointmentDetail$: Observable<AppointmentDetails>;
  appointmentDetail: AppointmentDetails;
  pageTitle = 'Some Default Title Maybe';

  constructor(
    private route: ActivatedRoute,
    private title: Title,
    private apiService: APIService
  ) {
    this.appointmentDetailId = this.id;
    console.log(this.appointmentDetailId);
  }

  ngOnInit() {
    this.route.paramMap
      .pipe(
        tap((params: ParamMap) => {
          this.id = params.get('id');
          // Or this.id = +params.get('id'); to coerce to type number maybe
          this.pageTitle = 'Termin Details: ' + this.id;
          this.title.setTitle(this.pageTitle);
        }),
        switchMap(() => this.apiService.getAppointmentDetailsById(this.id))
      )
      .subscribe((data: AppointmentDetails) => {
        this.appointmentDetail = data;
        console.log(this.appointmentDetail);
      });
  }

  cancelAppointment() {
    console.log(this.appointmentDetailId);
    this.apiService.cancelUserAppointment(this.appointmentDetailId);
  }
}

In the constructor you set this.appointmentDetailId = this.id which sets the value to the initial value of this.id (probably undefined ); 在构造设置this.appointmentDetailId = this.id该值设定为初始值this.id (可能undefined );

Later you set this.id = params.get('id') , but this will not change this.appointmentDetailId , because they're two different objects. 稍后,您设置this.id = params.get('id') ,但这不会更改this.appointmentDetailId ,因为它们是两个不同的对象。


If you want this.appointmentDetailId to always match this.id you should make it a simple wrapper, rather than its own object. 如果希望this.appointmentDetailId始终与this.id匹配,则应使其成为一个简单的包装器,而不是其自己的对象。

export class AppointmentDetailComponent implements OnInit {
  id: any;
  get appointmentDetailId() {
    return this.id;
  }
  set appointmentDetailId(value: any) {
    this.id = value;
  }

  // Other code
}

Using a custom get and set method, you can still access this.appointmentDetailId as if it were it's own field - but it will actually be the same as this.id . 使用自定义的get and set方法,您仍然可以访问this.appointmentDetailId ,就好像它是它自己的字段一样,但实际上它与this.id相同。 Now, any changes to either field will always be in sync. 现在,对任一字段的任何更改将始终保持同步。

// Always true
this.appointmentDetailId === this.id

this.appointmentDetailId = 123;
this.appointmentDetailId === 123; // True
this.id === 123; // True;

this.id = "Test";
this.appointmentDetailId === "Test"; // True
this.id === "Test"; // True

Alternatively, if you can simply omit the set method and then you'll just be able to access the same value, but you cannot change it. 另外,如果您可以简单地省略set方法,那么您将只能访问相同的值,但不能更改它。

// Always true
this.appointmentDetailId === this.id

this.appointmentDetailId = 123; // Error

this.id = "Test";
this.appointmentDetailId === "Test"; // True
this.id === "Test"; // True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM