Angular 12 - 类型错误:无法读取 null 的属性(读取“writeValue”)
我正在创建一个通用文本输入组件,在为项目提供服务时一切正常,但在构建的项目中我收到此错误:
类型错误:无法读取 null 的属性(读取“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>
Run Code Online (Sandbox Code Playgroud)
组件.ts
import { Component, Input, OnInit, Self } from '@angular/core';
import {AbstractControl, …Run Code Online (Sandbox Code Playgroud) 我有一个根据本文制作的自定义输入:medium.com:dont-reinvent-the-wheel。
\n这是我的代码,它处于严格模式 \xe2\x96\xbc
\n// input.component.ts\n\nimport { Component, Input, ViewChild } from \'@angular/core\';\nimport {\n ControlContainer,\n ControlValueAccessor,\n FormControl,\n FormControlDirective,\n NG_VALUE_ACCESSOR\n} from \'@angular/forms\';\nimport {\n FloatLabelType,\n MatFormFieldAppearance\n} from \'@angular/material/form-field\';\n\n@Component({\n selector: \'app-input\',\n templateUrl: \'./input.component.html\',\n styleUrls: [\'./input.component.scss\'],\n providers: [\n {\n provide: NG_VALUE_ACCESSOR,\n useExisting: InputComponent,\n multi: true\n }\n ]\n})\nexport class InputComponent implements ControlValueAccessor {\n isDisabled!: boolean;\n\n @Input() isRequired!: boolean;\n\n @Input() label!: string;\n\n @Input() placeholder!: string;\n\n @Input() readonly!: boolean;\n\n @Input() appearance: MatFormFieldAppearance = \'fill\';\n\n @Input() floatLabel: FloatLabelType = \'auto\';\n\n @ViewChild(FormControlDirective, { static: true })\n …Run Code Online (Sandbox Code Playgroud) angular angular-reactive-forms controlvalueaccessor angular-controlvalueaccessor