Winkel-TS-Fehler TS1109
Ich erhalte folgenden Fehler in der Konsole:
Webpack: Kompilieren ... 10% Gebäudemodule 0/1 Module 1 aktiv ... p / Einkaufsliste2 / src / app / app.module.tsERROR in src / app / Mahlzeit.service.ts (46,56): Fehler TS1109 : Ausdruck erwartet.
Datum: 2017-12-31T09: 55: 50.224Z
Hash: 8003d6d8f334085afa7f Zeit: 267 ms chunk {inline} inline.bundle.js (inline) 5,79 kB [Eintrag] chunk {main} main.bundle.js (main) 73,1 kB [ initial] [gerendert] chunk {polyfills} polyfills.bundle.js (polyfills) 558 kB [initial] chunk {styles} styles.bundle.js (styles) 456 kB [initial] chunk {vendor} vendor.bundle.js (vendor ) 11,3 MB [initial]Webpack: Erfolgreich kompiliert.
Ich sehe das Problem nicht. Der Code basiert auf dem Angular HTTP-Beispiel von https://angular.io/tutorial/toh-pt6 . Ich benutze Angular ^ 5.0.0, rxjs ^ 5.5.2
Code von food.service.ts:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { catchError, map, tap } from 'rxjs/operators';
import { of } from 'rxjs/observable/of';
import { HttpClient, HttpHeaders,
HttpParams } from '@angular/common/http';
import { MessageService } from './message.service';
import { Meal, oneMeal, Meals } from './meal';
const httpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'})
};
@Injectable()
export class MealService {
constructor(
private http: HttpClient,
private messageService: MessageService
) { };
private mealUrl = 'http://192.168.178.11:1337/api/meal';
private log(message: string) {
this.messageService.add('MealService: ' + message);
};
private handleError<T> (operation = 'operation', result?: T) {
return (error: any): Observable<T> => {
console.error(error)
this.log(`${operation} failed: ${error.message}`);
return of(result as T);
};
}
getMeals (page:number): Observable<Meals> {
let httpParams = new HttpParams().set('where', '%')
.set('orderBy', 'meal_id')
.set('page', page.toString())
.set('items', '10');
//this.messageService.add('Debug: ' + httpParams);
return this.http.get<Meals>(this.mealUrl, { params: httpParams })
.pipe(
tap(meals => this.log(`fetched ${meals.count} entries`)),
catchError(this.handleError('getMeals', <Meals>)) // Line 46: the Error
);
};
getMeal (id:number): Observable<oneMeal> {
let httpParams = new HttpParams().set('id', id.toString());
//this.messageService.add(`MealService: fetched meal_id=${id}`);
return this.http.get<oneMeal>(this.mealUrl, { params: httpParams })
.pipe(
tap(meal => this.log(`fetched ${meal.data[0].meal_name}`))//,
//catchError(this.handleError('getMeal', <oneMeal>))
);
}
}
Sie rufen die generische Methode falsch auf, damit Sie diese erhalten -
FEHLER in src / app / Mahlzeit.service.ts (46,56): Fehler TS1109: Ausdruck erwartet
Falscher Weg
catchError(this.handleError('getMeals', <Meals>)) // Line 46: the Error
Der richtige Weg
catchError(this.handleError<Meals>('getMeals'))
getMeals (page:number): Observable<Meals> {
let httpParams = new HttpParams().set('where', '%')
.set('orderBy', 'meal_id')
.set('page', page.toString())
.set('items', '10');
//this.messageService.add('Debug: ' + httpParams);
return this.http.get<Meals>(this.mealUrl, { params: httpParams })
.pipe(
tap(meals => this.log(`fetched ${meals.count} entries`)),
catchError(this.handleError<Meals>('getMeals')) // Line 46: the Error
);
};