Angular抽象类HttpClient注入 [英] Angular abstract class HttpClient injection
问题描述
大家好,我是Angular的新手,但我在Injectable类方面遇到了麻烦.
Hi everyone, i'm new with Angular and I am having troubles with Injectable classes.
我有一个抽象类(在web.ts上),该类的受保护属性之一是HttpClient对象.该抽象类的定义如下:
I have an abstract class (on web.ts) which one of its protected attributes is an HttpClient object. The definition of this abstract class is as follows:
import { HttpClient } from "@angular/common/http";
export abstract class Web {
protected baseURL: string;
constructor(baseURL: string, protected http: HttpClient) {
this.baseURL = baseURL;
}
public abstract RetrieveData(pattern: string);
}
然后,我有另一个类(在webA.ts上)扩展了Web类:
Then I have another class (on webA.ts) which extends the Web class:
import { Web } from './web';
export class WebA extends Web {
constructor() {
super("https://www.test.com");
}
private Search(pattern: string) {
pattern = pattern.replace(" ", "+");
var searchURL = this.baseURL + `/site/searchpage.jsp?st=${pattern}&id=pcat17071`;
console.log(searchURL);
this.http.get(searchURL).subscribe(data => {
console.log(data);
});
}
public RetrieveData(pattern: string) {
this.Search(pattern);
}
}
最后,在我的网站的组件类中,我想将WebA实例化为:
Finally, in a class of a component for my website I want to instantiate WebA as:
private webA: Web = new WebA();
它告诉我,我需要在webA.ts中的super("https://www.test.com");
上传递HttpClient对象.要解决此问题,我可以使WebA的构造函数如下:
It tells me that I need to pass an HttpClient object on super("https://www.test.com");
in webA.ts. To fix it, I can make the constructor for WebA like:
constructor(@Inject(HttpClient) http: HttpClient) {
super("https://www.test.com", http);
}
但这会导致我执行private webA: Web = new WebA(this.http);
强制我进入组件类:
But this would lead me to do private webA: Web = new WebA(this.http);
forcing me to have on my component class:
import { HttpClient } from "@angular/common/http";
...
constructor(private http: HttpClient) {}
我知道必须有另一种方法,因为我认为这破坏了我班级的层次结构
I know there must be another way to do it because in my opinion this is breaking hierarchy on my classes
TL; DR:
我需要在抽象类上自动注入" HttpClient,而不需要将此对象作为扩展程序类中super的参数传递.
TL;DR:
I need to "auto-inject" an HttpClient on an abstract class to not to need passing this object as an argument on super in the extender class.
推荐答案
要么删除WebA
export class WebA extends Web {
//constructor() {
// super("https://www.test.com");
//}
或重复构造函数参数,并使用super()
or repeat the constructor parameters and forward them with super()
export class WebA extends Web {
constructor(baseURL: string, protected http: HttpClient) {
super(baseURL, http);
}
如果在派生类(WebA
)中需要构造函数,则唯一的方法是重复并转发参数.
If you need a constructor in the derived class (WebA
) the only way is to repeat the parameters and forward them.
更新
使用new Xxx()
创建实例时,没有自动注入之类的功能.依赖注入仅适用于Angulars DI为您创建的实例.
There is no such thin as auto-inject when you create an instance with new Xxx()
. Dependency injection works only for instances that Angulars DI creates for you.
否则,请参见上面的答案.
Otherwise see my answer above.
这篇关于Angular抽象类HttpClient注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!