属性“listingSub$”没有初始值设定项,并且在构造函数中没有明确分配。ts(2564) 请解决这个问题

时间:2021-04-15 11:16:39

标签: angular mongodb express typescript-typings

import { Subscription } from "rxjs";

@Component({
  selector: 'app-listing-detail',
  templateUrl: './listing-detail.component.html',
  styleUrls: ['./listing-detail.component.scss']
}) 
export class ListingDetailComponent implements OnInit {
  id: string;
  listing!: Listing | undefined;
  listingSub$: Subscription | undefined;
  thisid: string | undefined;
  
  constructor(private listingService: ListingService, private route: ActivatedRoute) {}

  ngOnInit() {
    this.id = this.route.snapshot.paramMap.get("id");
    this.listingSub$ = this.listingService.getListing(this.id).subscribe(listing => {
      this.listing = listing;
    });
  }
}

1 个答案:

答案 0 :(得分:0)

错误包含答案。您需要该属性始终具有值。解决方案是默认分配 listingSub$ = undefined

export class ListingDetailComponent implements OnInit {
  ...
  listingSub$: Subscription | undefined = undefined;
  ...
}