在我自己的库中将 FormControl 添加到 FormGroup 时,formGroup.getRawValue() 出错。
import {
Component,
OnInit
} from '@angular/core';
import {
FormBuilder,
FormGroup
} from '@angular/forms';
import {testfunc} from 'test-lib'; // import a function from my own library.
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit{
constructor(
private formBuilder: FormBuilder,
) {
}
ngOnInit() {
const jsonA = {
a: 'test',
b: 10,
};
const fg = this.formBuilder.group(jsonA);
testfunc(fg); // Add a Form Control inside this function
const jsonB = fg.getRawValue(); // 'control.getRawValue is not a function' error
}
}
test-lib
是像这篇文章一样构建的 Angular 库项目。 testfunc
的代码如下。
import {
FormControl,
FormGroup
} from '@angular/forms';
export function testfunc(fg: FormGroup) {
fg.addControl('c', new FormControl('newcontrol'));
}
fg.getRawValue()
抛出如下错误。
ERROR TypeError: control.getRawValue is not a function
at forms.js:5460
at forms.js:5582
at forms.js:5508
at Array.forEach (<anonymous>)
at FormGroup._forEachChild (forms.js:5504)
at FormGroup._reduceChildren (forms.js:5576)
at FormGroup.getRawValue (forms.js:5453)
at AppComponent.ngOnInit (app.component.ts:34)
:
当
testfunc
与AppComponent
在同一个项目中时,我没有看到这个错误。但是当函数在外部库中时,我得到了这个错误。
当
testfunc
在外部库中时,为什么我会收到此错误消息?
Angular 版本是 9.1.11.
嘿,你有解决这个问题的办法吗??我也面临类似的问题,非常感谢您的帮助!!