简体   繁体   English

数组返回为未定义-依赖注入? 角度/打字稿

[英]Array returning as undefined - Dependency Injection? Angular/Typescript

I assign an array data received from an endpoint. 我分配从端点接收的数组数据。

I try to call this array in another file and it doesn't like it! 我尝试在另一个文件中调用此数组,但它不喜欢它! My subsidiaryNames array consoles correctly. 我的subscriberNames阵列控制台正确。

My first file: 我的第一个文件:

export class EngagementService {
    public orgSubSidNames = [];


    let subsidiaryNames = []
    data.forEach(sub => {
        console.log(sub.SubsidiaryName)
        subsidiaryNames.push(sub.SubsidiaryName)
    })
    subsidiaryNames = subsidiaryNames.filter(this.onlyUnique);
    console.log('subsidiary name array' + subsidiaryNames)
    this.orgSubSidNames = subsidiaryNames;

    // This is just a sample script. Paste your real code (javascript or HTML) here.

    if ('this_is' == /an_example/) {
        of_beautifier();
    } else {
        var a = b ? (c % d) : e[f];
    }

Second File: 第二档:

export class EngagementFilterComponent implements OnInit {
    public subSidNameArray: EngagementService;


    updateSub(data) {
        console.log(data);
        console.log('attempting to print the subsid array' + this.subSidNameArray.orgSubSidNames)
    }

I get the error: 我收到错误:

 Cannot read property 'orgSubSidNames' of undefined

您永远不会设置属性的值public subSidNameArray: EngagementService;

In the code that creates EngagementFilterComponent 在创建EngagementFilterComponent的代码中

const filter = new EngagementFilterComponent()
filter.subSidNameArray = new EngagementService()

or set it in constructor 或在构造函数中设置

public subSidNameArray: EngagementService = new EngagementService()

or use injection 或使用注射

If your framework uses injection, make sure your receiving component uses a proper constructor. 如果您的框架使用注入,请确保您的接收组件使用适当的构造函数。 constructor(private engagementService: EngagementService)

Explanation 说明

The line 线

 public subSidNameArray: EngagementService

declares that your class contains an instance of EngagementService . 声明您的类包含EngagementService的实例。 You have not created one yet. 您尚未创建一个。 An instance of the class must be created using the new keyword. 必须使用new关键字创建该类的实例。 The :EngagementService part is typescript for declaring the type that subSidNameArray is, a feature not found in ordinary javascript . :EngagementService部分是用于声明subSidNameArray是类型的typescript ,这是普通javascript找不到的功能。 It does not create an instance though. 但是,它不会创建实例。

subSidNameArray needs to be initialized somewhere, not just declared. subSidNameArray需要在某个地方初始化,而不仅仅是声明。 As others have answered, you can make an explicit assignment to it with new EngagementService() , but if you use Angular, the "correct" way to do it would be to use dependecy injection (DI) in your second file: 正如其他人回答的那样,您可以使用new EngagementService()对其进行显式分配,但是如果您使用Angular,则“正确”的做法是在第二个文件中使用依赖注入(DI):

constructor(private engagementService: EngagementService) {...}

Make sure that your EngagementService is injectable, by adding the @Injectable decorator before the class declaration: 通过在类声明之前添加@Injectable装饰器,确保您的EngagementService是可@Injectable

@Injectable({
    providedIn: 'root'
})
export class EngagementService {
  ...
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM