简体   繁体   中英

Property 'clientId' is used before its initialization

In my component i have declared

export class UserPatientsReportFormComponent implements OnInit {
clientId: number ;

this is my oninit function

ngOnInit(): void {
    this.activatedRoute.queryParams.subscribe(params => {
      
      this.clientId = params.id == undefined ? null : this.commonService.encryptValue(params.id, false);
      
    });

and this is where i tried to use this clientId and face the issue.Below in patientId i tried to use this.clientId.Here i face the issue. how should i fixed it

patientReportForm=this.getGroup({patientId : this.clientId ,hubxCategoryId:"",notes:""})
getGroup(data:any=null)
{
  data=data || {patientId : 0,hubxCategoryId :"",notes:""}
  return new FormGroup({
    patientId:new FormControl(data.patientId),
    hubxCategoryId :new FormControl(data.hubxCategoryId),
    notes:new FormControl(data.notes)
  })
}

this.clientId value comes at other line of code but not here. when i use in the below code this.clientId works. but no in the above code that i shared

this.patientReportForm = this.formBuilder.group({
       patientId : new FormControl(Number(this.clientId)),
      hubxCategoryId: ['',Validators.required],

this is my model.Here clientId holds the patientId value.

export class HubxModel{ 
    id: number;
    categoryId: number;
    itemTitle: string;
    itemUnit: string;
    isActive: boolean=true;
    itemValue: string;
    patientId: number;
    isDeleted: boolean;
}

here clientId value has received but when i submit patientid is null 客户编号

As error says, you are using the value of clientId before its initialisation, so try doing all the things after you set the value of this.clientId in queryParams subscribe.

can help more accurately if you share your component.ts file.

this because the strict is enabled in tscconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,  🤯🤯

so you will force to set an initial value to class property

clientId: number = 0;

you can read more about this here strict and this rule strictPropertyInitialization

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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