Angular instanceof在角度库中创建时返回false
在角度库中创建对象时,Angular instanceof在角度库中创建时返回false,angular,typescript,angular-library,Angular,Typescript,Angular Library,在角度库中创建对象时,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({
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');
}
}
}
testlib
是一个角度库项目,构建为。genControl
的代码如下所示
import {FormControl} from '@angular/forms';
export function genControl(): FormControl {
return new FormControl('');
}
上面代码的输出如下所示
fcA is not instanceof FormControl
fcB is instanceof FormControl
"compilerOptions": {
:
"paths": {
"@angular/*": [
"./node_modules/@angular/*"
]
}
},
为什么fcA
不是FormControl
的实例?如何修复fcA
将是FormControl
的实例
Angular版本在主项目AppComponent和Angular Library之间是相同的,后者是9.1.11。正如@andriishupta所提到的,这是因为应用程序和库从不同的文件导入
FormControl
。
我应该在tsconfig.json
中指定路径,如下所示
fcA is not instanceof FormControl
fcB is instanceof FormControl
"compilerOptions": {
:
"paths": {
"@angular/*": [
"./node_modules/@angular/*"
]
}
},
正如@andriishupta提到的,这是因为应用程序和lib从不同的文件导入FormControl
。
我应该在tsconfig.json
中指定路径,如下所示
fcA is not instanceof FormControl
fcB is instanceof FormControl
"compilerOptions": {
:
"paths": {
"@angular/*": [
"./node_modules/@angular/*"
]
}
},
我不确定,但我想我也有类似的想法:如果FormControls来自不同的地方,这可能是正确的。例如,您的库是构建的-它的FormControl与您在执行期间测试的FormControl不同。您可以通过调试进行检查:在运行时检查FormControls的提交位置-我确定它们来自不同的文件我不确定,但我认为我有类似的情况:如果FormControls来自不同的位置,这可能是正确的。例如,您的库是构建的-它的FormControl与您在执行期间测试的FormControl不同。您可以通过调试进行检查:在运行时检查FormControls的提交位置-我确信它们来自不同的文件