我是angular的新手,我正在尝试将angular 4升级到最新的ngular, 我试图使可重用的类方法可以在许多服务和组件上使用
让这个文件叫:
./ base-services.services.ts :
getDataParam(url: any, params: any) {
return this.http
.get(url, { params: params })
.pipe(catchError(this.handleError))
.subscribe(response => response);
}
createData(url: any, data: any) {
return this.http
.post(url, data)
.pipe(catchError(this.handleError))
.subscribe(response => response);
}
并且我使用该基本服务扩展了我提供的所有服务
./ getData.services.ts :
export class LegalorderService extends BaseService {
private token: string;
constructor(http: HttpClient, auth: AuthService) {
super(http);
auth.isAuthenticated();
this.token = auth.token;
}
getCart() {
let params = { access_token: this.token };
return this.getDataParam(
Configuration.BASE_URL + Configuration.GET_SAVED_CART,
params
);
}
}
以及组件上的
updateValue() {
this.legalOrderService.getCart().subscribe(response => {
if (response.message != "ERROR") {
this.localStorageService.store("legalOrder", response.result);
this.localStorageService.store(
"cartItems",
response.result.order_details
);
this.cartTotal = this.localStorageService.retrieve("cartItems");
}
});
}
我在服务上使用该组件上的订阅错误吗?
然后我得到这样的错误:
ERROR in node_modules/rxjs/internal/Subscription.d.ts(16,3): error TS2374: Duplicate string index signature.
node_modules/rxjs/internal/Subscription.d.ts(17,3): error TS2393: Duplicate function implementation.
node_modules/rxjs/internal/Subscription.d.ts(17,44): error TS1183: An implementation cannot be declared in ambient contexts.
node_modules/rxjs/internal/Subscription.d.ts(20,3): error TS2393: Duplicate function implementation.
node_modules/rxjs/internal/Subscription.d.ts(20,44): error TS1183: An implementation cannot be declared in ambient contexts.
node_modules/typescript/lib/lib.es5.d.ts(124,5): error TS2411: Property 'constructor' of type 'Function' is not assignable to string index type 'string'.
node_modules/typescript/lib/lib.es5.d.ts(127,5): error TS2411: Property 'toString' of type '() => string' is not assignable to string index type 'string'.
node_modules/typescript/lib/lib.es5.d.ts(130,5): error TS2411: Property 'toLocaleString' of type '() => string' is not assignable to string index type 'string'.
node_modules/typescript/lib/lib.es5.d.ts(133,5): error TS2411: Property 'valueOf' of type '() => Object' is not assignable to string index type 'string'.
node_modules/typescript/lib/lib.es5.d.ts(139,5): error TS2411: Property 'hasOwnProperty' of type '(v: string | number | symbol) => boolean' is not assignable to string index type 'string'.
node_modules/typescript/lib/lib.es5.d.ts(145,5): error TS2411: Property 'isPrototypeOf' of type '(v: Object) => boolean' is not assignable to string index type 'string'.
node_modules/typescript/lib/lib.es5.d.ts(151,5): error TS2411: Property 'propertyIsEnumerable' of type '(v: string | number | symbol) => boolean' is not assignable to string index type 'string'.
答案 0 :(得分:1)
您正在尝试订阅:
getDataParam
updateValue
中再次订阅从subscribe
中删除getDataParam
:
getDataParam(url: any, params: any) {
return this.http
.get(url, { params: params })
.pipe(catchError(this.handleError));
}
答案 1 :(得分:0)
我在您的代码中看到的一个问题是,您不需要多次订阅,只需从getDataParam方法中删除订阅,因为您已经在订阅该组件。
getDataParam(url: any, params: any) {
return this.http
.get(url, { params: params })
.pipe(catchError(this.handleError));
}