【发布时间】:2020-09-16 22:04:39
【问题描述】:
在 Angular 库中创建对象时,instanceof 返回 false。
import {
Component,
OnInit
} from '@angular/core';
import {
FormControl,
} from '@angular/forms';
import {genControl} 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() {
}
ngOnInit() {
const fcA = genControl(); // Create a FormControl in Angular Library
const fcB = new FormControl('');
if (fcA instanceof FormControl) {
console.log('fcA is instanceof FormControl');
} else {
console.log('fcA is not instanceof FormControl'); // displays this.
}
if (fcB instanceof FormControl) {
console.log('fcB is instanceof FormControl'); // displays this.
} else {
console.log('fcB is not instanceof FormControl');
}
}
}
test-lib 是一个 Angular 库项目,构建为 this post。 genControl的代码如下。
import {FormControl} from '@angular/forms';
export function genControl(): FormControl {
return new FormControl('');
}
上面代码的输出如下。
fcA is not instanceof FormControl
fcB is instanceof FormControl
为什么fcA 不是FormControl 的实例?我应该如何修复fcA 将成为FormControl 的实例?
主项目AppComponent和Angular库genControl的Angular版本相同,即9.1.11。
【问题讨论】:
-
我不确定,但我想我有类似的情况:如果 FormControls 来自不同的地方,这可能是正确的。例如您的库已构建 - 它的 FormControl 与您在执行期间测试的 FormControl 不同。您可以通过调试来检查它:在运行时检查 FormControls 的来源 - 我确信它们来自不同的文件
标签: angular typescript angular-library