【问题标题】:Angular 12 - TypeError: Cannot read properties of null (reading 'writeValue')Angular 12 - TypeError:无法读取 null 的属性(读取“writeValue”)
【发布时间】:2021-11-24 04:57:02
【问题描述】:

Angular 12 - TypeError:无法读取 null 的属性(读取“writeValue”)

我正在创建一个通用文本输入组件,在为项目提供服务时一切正常,但在构建的项目中出现此错误:

TypeError: Cannot read properties of null (reading 'writeValue')

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>

组件.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 CLI:12.2.6 节点:14.17.3 包管理器:npm 6.14.13 操作系统:win32 x64 角度:12.2.6

【问题讨论】:

    标签: angular angular-reactive-forms angular-controlvalueaccessor


    【解决方案1】:

    在构造函数中使用@Optional() 并进行空值检查:

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

    另外,将正文添加到方法中

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 2021-12-04
      • 2021-12-17
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多