微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

反映元数据设计:paramtypes 返回一个数组,其中包含未定义的元素

如何解决反映元数据设计:paramtypes 返回一个数组,其中包含未定义的元素

我正在尝试获取某个类的构造函数参数,但是 获取一个包含未定义元素的数组。

为了测试,我使用服务 Example2Services 和 ExampleService

Example2Service.ts

import { ExampleService } from './example.service';


   export class Example2Service {
      constructor(private es1: ExampleService) {}

      getValue() {
        return "some value2";
      }
    }

ExampleService.ts

import { Example2Service } from './example2.service';

export class ExampleService {
    constructor(private es2: Example2Service) {    }

    getValue() {
        return this.es2.getValue();
    }
    getString() {
        return "1"
    }
}

在这函数中我发送一个 Example2Service

private resolve(target: Type): Object | Function {

    let tokens = Reflect.getMetadata('design:paramtypes',target) 
    ...
}

调试代码,我得到这个令牌变量

enter image description here

为了理解行为,我尝试直接从某些类中获取参数

import {Example2Service} from '../../exaple2.service.ts';
import {ExampleService} from '../../exaple.service.ts';

private resolve(target: Type): Object | Function {

    let tokensA = Reflect.getMetadata('design:paramtypes',Example2Service)
    let tokensB = Reflect.getMetadata('design:paramtypes',ExampleService);
    let tokens = Reflect.getMetadata('design:paramtypes',target);
    ...
}

调试这个案例我得到了这个,发送一个 Example2Service 来解析函数。现在 token 是一个数组,里面有一个 Class。请注意,在导入 Example2Service 和 ExampleService 时会发生这种情况,但是当更改导入顺序时,行为会发生变化

enter image description here


编辑

我发现当依赖之间存在循环时会发生这种情况

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。