简体   繁体   English

Angular 12 - 类型错误:无法读取 null 的属性(读取“writeValue”)

[英]Angular 12 - TypeError: Cannot read properties of null (reading 'writeValue')

Angular 12 - TypeError: Cannot read properties of null (reading 'writeValue') Angular 12 - 类型错误:无法读取 null 的属性(读取“writeValue”)

I'm creating a generic text input component, everything works well while serving the project, but in the built project I get this error:我正在创建一个通用文本输入组件,在为项目服务时一切正常,但在构建的项目中我收到此错误:

TypeError: Cannot read properties of null (reading 'writeValue')类型错误:无法读取 null 的属性(读取“writeValue”)

HTML: HTML:

<div class="p-field">
    <label>{{label}} <span class="p-error">{{checkRequired(ngControl.control) ? '*' : ''}}</span></label>
    <input class="full-width" [type]="type" pInputText [formControl]="ngControl.control">

    <small *ngIf="ngControl.control.touched && ngControl.control.errors?.required" class="p-error">{{label}} is required</small>


    <small *ngIf="ngControl.control.errors?.minlength" class="p-error">
        {{label}} must be at least {{ngControl.control.errors.minlength['requiredLength']}}
    </small>

    <small *ngIf="ngControl.control.errors?.maxlength" class="p-error">
        {{label}} must be at most {{ngControl.control.errors.maxlength['requiredLength']}}
    </small>
    <small *ngIf="ngControl.control.errors?.email" class="p-error">
        This email is not valid
    </small>

    <small *ngIf="ngControl.control.errors?.isMatching" class="p-error">Passwords do not match</small>
</div>

component.ts组件.ts

import { Component, Input, OnInit, Self } from '@angular/core';
import {AbstractControl, ControlValueAccessor, NgControl} from '@angular/forms';

@Component({
  selector: 'app-text-input',
  templateUrl: './text-input.component.html',
  styleUrls: ['./text-input.component.css']
})
export class TextInputComponent implements ControlValueAccessor {
  @Input() label: string;
  @Input() type = 'text';

  constructor(@Self() public ngControl: NgControl) {
    this.ngControl.valueAccessor = this;
  }

  checkRequired(control) {
    if (control.validator) {
      const validator = control.validator({} as AbstractControl);
      if (validator && validator.required) {
        return true;
      }
    }
  }

  writeValue(obj: any): void {
  }

  registerOnChange(fn: any): void {
  }

  registerOnTouched(fn: any): void {
  }

}

Angular info: Angular 信息:

Angular CLI: 12.2.6 Node: 14.17.3 Package Manager: npm 6.14.13 OS: win32 x64 Angular: 12.2.6 Angular CLI:12.2.6 节点:14.17.3 Package 管理器:npm 6.14.13 操作系统:win32 x64 Angular:12.2.6

The error occurs when provider setup is missing缺少提供程序设置时发生错误

  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      multi:true,
      useExisting: CUSTOMER_INPUT_CLASS_HERE
    }
  ]  

In your case在你的情况下

@Component({
  selector: 'app-text-input',
  templateUrl: './text-input.component.html',
  styleUrls: ['./text-input.component.css'],
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      multi:true,
      useExisting: TextInputComponent
    }
  ]
})
export class TextInputComponent implements ControlValueAccessor {
...
}

Maybe a bit late but it might help someone here:也许有点晚了,但它可能会帮助这里的人:

I got this error when I attached formControlName to mat-radio-button instead to mat-radio-group .当我将formControlName附加到mat-radio-button而不是mat-radio-group时出现此错误。

In constructor use @Optional() and do null check :在构造函数中使用@Optional()并进行空检查:

constructor(@Optional() @Self() public ngControl: NgControl) {
   if (this.ngControl != null) this.ngControl.valueAccessor = this;
}

Also, add body to methods like此外,将正文添加到方法中,例如

writeValue(value: number): void {
    this.value = value;
    this.onChange(this.value);
}

onChange: (_: any) => void = (_: any) => {};

onTouched: () => void = () => {};

registerOnChange(fn: any): void {
    this.onChange = fn;
}

registerOnTouched(fn: any): void {
    this.onTouched = fn;
}

I got this error when i tried to make the authorsiation check in the backend part of the validation logic.当我尝试在验证逻辑的后端部分进行授权检查时出现此错误。 I declared the submit button in the formbuilder group.我在 formbuilder 组中声明了提交按钮。 When I used an input with style="display: none" all was fine.当我使用带有 style="display: none" 的输入时,一切都很好。

就我而言,出现了特定错误,因为我没有在模块导入中添加特定组件。

Check module.ts.检查 module.ts。 We need to import DropDownsMoudle to user dropdownlists.我们需要将 DropDownsMoudle 导入到用户下拉列表中。

import { DropDownsModule } from "@progress/kendo-angular-dropdowns";从“@progress/kendo-angular-dropdowns”导入 {DropDownsModule};

I would suggest that you create a module called SharedModule, where you will declare all your shared components in the declarations array.我建议您创建一个名为 SharedModule 的模块,您将在声明数组中声明所有共享组件。 in your exports array, export the custom text input component.在您的导出数组中,导出自定义文本输入组件。 And then where you need to use the component, import the shared module into your app.module.然后在需要使用该组件的地方,将共享模块导入到您的 app.module 中。 Remember to remove the input declaration in app.module and only import the sharedModule.记得去掉 app.module 中的 input 声明,只导入 sharedModule。 Also remember to import the ReactiveFormsModule and the FormsModule in your sharedModule.ts还要记住在 sharedModule.ts 中导入 ReactiveFormsModule 和 FormsModule

暂无
暂无

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

相关问题 TypeError:无法读取 null 的属性(读取“writeValue”) - TypeError: Cannot read properties of null (reading 'writeValue') Angular 12:错误类型错误:无法读取未定义的属性(读取“nativeElement”) - Angular 12: ERROR TypeError: Cannot read properties of undefined (reading 'nativeElement') angular 错误类型错误:无法读取 null 的属性(读取“_rawValidators”) - angular ERROR TypeError: Cannot read properties of null (reading '_rawValidators') TypeError:无法读取 null 的属性(读取“scrollHeight”)| Angular 11 - TypeError: Cannot read properties of null (reading 'scrollHeight') | Angular 11 类型错误:无法读取 null 的属性(读取“数量”) - TypeError: Cannot read properties of null (reading 'quantity') 类型错误:无法读取 null 的属性(读取“文件名”) - TypeError: Cannot read properties of null (reading 'fileName') TypeError:无法读取 null 的属性(读取“_rawValidators”) - TypeError: Cannot read properties of null (reading '_rawValidators') Angular 12 - 显示类型错误:无法读取 Html 元素的未定义属性(读取“peersContainer”) - Angular 12 - Showing TypeError: Cannot read properties of undefined (reading 'peersContainer') for Html Element 获取 TypeError:在发送 ajax 请求(Angular 12)时无法读取未定义的属性(读取“管道”) - Getting TypeError: Cannot read properties of undefined (reading 'pipe') while sending ajax request (Angular 12) Angular:无法读取 null 的属性(读取“cannotContainSpace”) - Angular: Cannot read properties of null (reading 'cannotContainSpace')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM