简体   繁体   English

在 Angular 库中创建时,instanceof 返回 false

[英]instanceof returns false when created in Angular Library

When created an object in an Angular Library, instanceof returns false.在 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 is an Angular Library project build as this post . test-lib是一个 Angular 库项目构建作为这篇文章 The code for genControl is as below. genControl的代码如下。

import {FormControl} from '@angular/forms';

export function genControl(): FormControl {
  return new FormControl('');
}

The output of the code above is as follows.上面代码的输出如下。

fcA is not instanceof FormControl
fcB is instanceof FormControl

Why fcA is not an instance of FormControl ?为什么fcA不是FormControl的实例? How should I fix for fcA will be instance of FormControl ?我应该如何修复fcA将是FormControl实例?

Angular version is same between main project AppComponent and Angular Library genControl , which is 9.1.11.主项目AppComponent和 Angular 库genControl Angular 版本相同,即 9.1.11。

As @andriishupta mentioned, this is because the app and the lib imports FormControl from different files.正如@andriishupta 提到的,这是因为应用程序和库从不同的文件导入FormControl I should specify paths in tsconfig.json as follows.我应该在tsconfig.json指定paths如下。

  "compilerOptions": {
        :
    "paths": {
      "@angular/*": [
        "./node_modules/@angular/*"
      ]
    }
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 instanceof函数参数在Typescript / Angular中返回false - instanceof function parameter returns false in Typescript/Angular 为什么angular导航时返回false - Why does the angular returns false when navigating 引用库 angular 创建的服务时出错 - Error when referencing service created by library angular Angular 8 Observable返回“ _isScalar:false…” - Angular 8 Observable Returns “_isScalar:false…” Angular CLI - 使用“ng new library”创建的自定义库时“找不到模块” - Angular CLI - 'Cannot find module' when using custom library created with 'ng new library' 在angular2中,我使用的是canActivate,但是当它返回false时,它仍然会转到该路线 - In angular2 I'm using canActivate but when it returns false it still goes to the route Angular 11 - 模态方法检查数组中的值并返回 false,即使构造函数加载了数组 - Angular 11 - Modal Method checks for value in array and returns false even when the constructor loads the array 使用--watch = false的Angular CLI 6覆盖返回错误 - Angular CLI 6 coverage with --watch=false returns error canActivate返回false并仍然转到屏幕Angular 2 - canActivate returns false and still goes to screen Angular 2 Angular,Filter管道返回空数据/ false - Angular, Filter pipe returns empty data/false
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM