与Typescript结合使用Angular 1.x时,我面临一些问题。
考虑此代码:
get(id): ng.IPromise<Server.MyItem> {
return this.$http.get(`${this.baseAddress}/${id}`).then(d => d.data);
}
它是由TSC编译的,但是我会收到以下错误:
类型'iPromise&lt; {}>'不能分配给'ipromise'
我尝试使用更具体的 IHttpPromise
,但它不做技巧。
有什么想法?
尝试将数据投放到 Server.MyItem
:
get(id): ng.IPromise<Server.MyItem> {
return this.$http.get(`${this.baseAddress}/${id}`).then(d => <Server.MyItem>d.data);
}
您也可以使用该方法的通用版本:
get(id): ng.IPromise<Server.MyItem> {
return this.$http.get<Server.MyItem>(`${this.baseAddress}/${id}`).then(d => d.data);
}
当我使用Typescript时,这就是我和我的SR实现请求的方式。
首先,我需要两个从任何请求设置的结果设置。一个扩展了IHTTPPPROMISECALLBACKARG,另一个用于我的自定义输出。我一直喜欢为即将回来的实际类型创建第三个。在此示例中,我将联系人用作工厂,因此我需要一个ICOntact接口。
interface ICallback<T> extends ng.IHttpPromiseCallbackArg<T> {
data?: T;
}
interface IOutput<T> {
Output: T,
ErrorMsg: string
}
interface IContact {
ContactID: string;
ContactLastName: string;
ContactFirstName: string;
ContactEmail: string;
ContactPhone: string;
ContactFullName: string;
ModifiedBy?: string;
ModifiedOn?: Date;
CreatedBy: string;
CreatedOn: Date;
}
然后关注我需要一个通知工厂和请求工作工厂
export class NotificationFactory {
success = function (msg: string, title?: string): void {
this.toastr.success(msg, title);
}
error = function (msg: string, title?: string): void {
this.toastr.error(msg, title);
}
static $inject: Array<string> = ['toastr'];
constructor(private toastr: ng.toastr.IToastrService) {
return this;
}
}
export class RequestHelper {
toastSuccess = function (msg: string, title?: string): void {
this.NotificationFactory.success(msg, title);
}
toastError = function (msg: string, title?: string): void {
this.NotificationFactory.error(msg, title);
}
private success = (results: ICallback<IOutput<any>>, options: IOptions): any => {
if (results.data.ErrorMsg) {
this.NotificationFactory.error(results.data.ErrorMsg);
}
if (typeof options.callback === 'function') {
if (options.showSave && !results.data.ErrorMsg) {
this.NotificationFactory.success('Your changes have been saved.');
}
return options.callback(results.data);
}
if (options.showSave && !results.data.ErrorMsg) {
this.NotificationFactory.success('Your changes have been saved.');
}
return results.data;
}
private fail = (err: any, options: IOptions): ng.IPromise<any> => {
if (options.callingFn !== undefined) {
this.NotificationFactory.error('There was an error calling ' + options.callingFn + '. details:' + err.statusText);
}
return err;
}
makeRequest = (request: IRequest, o?: IOptions): ng.IPromise<any> => {
o = o || {};
var vm = this;
return this.$http(request).then((results: ICallback<IOutput<any>>) => {
return vm.success(results, o);
}).catch((err) => {
return vm.fail(err, o);
});
}
static $inject: Array<string> = ['$http', '$state', '$stateParams', 'NotificationFactory'];
constructor(private $http: ng.IHttpService, private $state: any, private $stateParams: IMyStateParams, private NotificationFactory: NotificationFactory) {
return this;
}
}
然后,在任何工厂中,我都可以注入我的请求助手并致电" makerequest'
export class ContactsFactory {
getData = (): ng.IPromise<IOutput<Array<IContact>>> => {
let req = { method: 'GET', url: '/Contacts/GetContacts?d=' + Date.parse(new Date().toString()).toString() };
return this.RequestHelper.makeRequest(req, { callingFn: 'ContactsFactory.getData', callback: null, showSave: false });
}
getContactByID = (id: string): ng.IPromise<IOutput<Array<IContact>>> => {
let req = { method: 'POST', url: '/Contacts/GetContactByID', data: { id: id } };
return this.RequestHelper.makeRequest(req, { callingFn: 'ContactsFactory.getContactByID', callback: null, showSave: false });
}
static $inject: Array<string> = ['RequestHelper', 'UsersFactory'];
constructor(private RequestHelper: RequestHelper, private UsersFactory: UsersFactory) {
return this;
}
}
和最后一个称我的工厂的控制器
export class ContactsController {
loading: boolean;
contacts: Array<IContact>;
initialize = (): void => {
this.loading = true;
this.ContactsFactory.getData().then((results) => {
this.contacts = results.Output;
this.loading = false;
});
});
}
static $inject: Array<string> = ['ContactsFactory'];
constructor(private ContactsFactory: ContactsFactory) {
this.initialize();
}
}
希望这会有所帮助。另外,我正在向@franks回答,因为我按照他的说法打字了。信用phxjoe寻求帮助,当时我的SR开发人员。