[英]How to inject downgraded Angular service into an AngularJS project?
但是,我真正想要升級的代碼庫已經為大多數服務提供了ts類,因此我遵循了官方指南 ,該指南將ts類替換為angularjs電話服務。 因此,現在我有了電話服務的ts類,但是當我用ng serve
運行應用程序時,電話服務是一個未知的提供程序。
"types": ["angular"]
到我的tsconfig中。 import { Injectable } from "@angular/core";
import { Observable } from "rxjs";
import { HttpClient } from "@angular/common/http";
import { IPhoneData } from "../../interfaces/IPhoneData";
// angular does not get resolved during compilation this way
// declare var angular: angular.IAngularStatic;
// Phone is an unknown provider this way
declare var angular: any;
import { downgradeInjectable } from '@angular/upgrade/static';
@Injectable()
export class Phone {
constructor(private http: HttpClient) { }
query(): Observable<IPhoneData[]> {
return this.http.get<IPhoneData[]>(`phones/phones.json`);
}
get(id: string): Observable<IPhoneData> {
return this.http.get<IPhoneData>(`phones/${id}.json`);
}
}
angular.module('core.phone')
.factory('phone', downgradeInjectable(Phone));
我如何才能將電話服務ts類注入angularjs組件中? 鏈接到我的倉庫 。
順便說一句,我輸掉了“電話”工廠的情況,應該像下面這樣(可觀察的東西也應該是應許的)。
export class Phone {
constructor(private http: HttpClient) { }
query(): Promise<IPhoneData[]> {
return this.http.get<IPhoneData[]>(`phones/phones.json`).toPromise();
}
get(id: string): Promise<IPhoneData> {
return this.http.get<IPhoneData>(`phones/${id}.json`).toPromise();
}
}
angular.module('core.phone')
.factory('Phone', downgradeInjectable(Phone));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.