[英]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.