简体   繁体   English

Angular 5了解外部类导入

[英]Angular 5 Understanding External Class Imports

I have created a class model for Schedules. 我为时间表创建了一个类模型。 I then import this class into my main program. 然后,将此类导入到我的主程序中。 My problem is that this class uses the HttpClient and the way I was taught to use it is to create the variable for it inside the constructor of the class. 我的问题是,此类使用HttpClient,而有人教我使用它的方法是在该类的构造函数中为其创建变量。 But the problem then comes in, when I create a new instance of the Schedule (newSchedule = new Schedule;) then it expects a parameter in the place of the HttpClient. 但是问题来了,当我创建Schedule的新实例(newSchedule = new Schedule;)时,它期望在HttpClient处放置一个参数。 How do I make it ignore the HttpClient when I want to create a new instance of the class? 当我要创建类的新实例时,如何使它忽略HttpClient?

Here is the Schedule model class: 这是Schedule模型类:

import { HttpClient } from '@angular/common/http';
export class Schedule {
    constructor(private httpClient: HttpClient) {}
}

But now I need to pass this.HttpClient in my main program, which of course is not needed: 但是现在我需要在我的主程序中传递this.HttpClient,当然不需要:

import { HttpClient } from '@angular/common/http';
export class AppComponent {
    constructor(private httpClient: HttpClient) {}
    var NewSchedule = new Schedule(this.HttpClient);
}

How do I remove the need to pass this.HttpClient? 如何消除传递this.HttpClient的需要? I assume my process is quite wrong 我认为我的过程很错

Your process seems indeed wrong. 您的过程似乎确实是错误的。 But you ask for help, so we provide. 但是您需要帮助,所以我们提供了。

You need to make the parameter of the constructor optional. 您需要使构造函数的参数为​​可选。 Do like so : 这样做:

export class Schedule {
  constructor(private http?: HttpClient){}
}

You can also give it a default value : 您也可以给它一个默认值:

export class Schedule {
  constructor(private http: HttpClient = null){}
}

After reading the comments, what I understood is, you want to have different instances of the Schedule class but still want to access the httpClient object. 阅读注释后,据我了解,您希望拥有Schedule类的不同实例,但仍想访问httpClient对象。

Doing the following may be one solution: 执行以下操作可能是一种解决方案:

Make a public method under the Schedule class: Schedule类下创建一个公共方法:

public getNewInstance(object: Object) {
    let obj = Object.create(object.constructor.prototype);
    object.constructor.apply(obj, [this.httpClient]);
    return obj;
}

Also make your Subject class injectable. 同时使您的Subject类可注射。 Please note, Your class will be an injectable and an singleton inside another classes, but you will have a way to get several instances using that singleton 请注意,您的课程将是可注入的,并且是另一个课程中的一个单例,但是您将可以使用该单例来获取多个实例

DO something like: 做类似的事情:

@Injectable()
export class Schedule ...

Then, In your appComponent, inject Subject . 然后,在您的appComponent中,注入Subject

export class AppComponent {
constructor(private subjectInstance: Subject) {}
...

private newSubjectInstance;
ngOnInit() {
    this.newSubjectInstance = this.subjectInstance.getNewInstance(this.subjectInstance);
    // so this should be a new instance but still have the httpClent inside it.
}

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

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