2017-12-31 18 views
2

난 다음 얻을 오류 :각도 TS 오류 TS1109

webpack: Compiling... 10% building modules 0/1 modules 1 active ...p/shoppinglist2/src/app/app.module.tsERROR in src/app/meal.service.ts(46,56): error TS1109: Expression expected.

Date: 2017-12-31T09:55:50.224Z
Hash: 8003d6d8f334085afa7f Time: 267ms chunk {inline} inline.bundle.js (inline) 5.79 kB [entry] chunk {main} main.bundle.js (main) 73.1 kB [initial] [rendered] 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: Compiled successfully.

내가 문제를 볼 수 없습니다. 코드는 https://angular.io/tutorial/toh-pt6의 Angular HTTP 예제를 기반으로합니다. meal.service.ts에서 나는 5.0.0^각도 사용하고 , rxjs^5.5.2

코드 :

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>)) 
    ); 
    } 
} 

답변

4

이 얻을 있도록 잘못된 방법으로 일반적인 방법을 요구하고있다 -

ERROR in src/app/meal.service.ts(46,56): error TS1109: Expression expected

잘못된 방법

catchError(this.handleError('getMeals', <Meals>)) // Line 46: the Error 

올바른 방법

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 
    ); 
    }; 
+0

는이 오류를 얻을 : 'SRC/응용 프로그램/meal.service.ts에서 ERROR (43,5) : 오류 TS2322 : 유형 '이 <{} | Meals> 관찰 가능한'를 입력 할 할당 할 수없는 '관찰 가능한 '. 유형 '{} | 식사 '는'식사 '유형에 지정할 수 없습니다. '{}'유형을 '식사'유형에 지정할 수 없습니다. '{}'유형에 'count'속성이 없습니다. src/app/meal.service.ts (46,56) : 오류 TS2345 : 'typeof Meals'유형의 인수를 '食事'유형의 매개 변수에 지정할 수 없습니다. 'typeof 식사'유형에 'count'속성이 없습니다. ' – Sven

+0

@seven : 답변을 업데이트했습니다. –

0

당신이, 당신이 매개 변수를받을 것으로 예상 것 알에 대해 참조 설명서의는 handleError 기능을 선택하면,

private handleError<T> (operation = 'operation', result?: T) { 
return (error: any): Observable<T> => { 

    // TODO: send the error to remote logging infrastructure 
    console.error(error); // log to console instead 

    // TODO: better job of transforming error for user consumption 
    this.log(`${operation} failed: ${error.message}`); 

    // Let the app keep running by returning an empty result. 
    return of(result as T); 
}; 
} 

하지만 코드에서 handleError 함수를 잘못 사용하고 있습니다. 오류 TS1109 : 표현 예상 SRC/응용 프로그램/meal.service.ts에서 ERROR (46,56) - 당신이 얻는 이유

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 
    ); 
    }; 

는 그입니다. 그래서 이것을 시도 - 도움

catchError(this.handleError< Meals >('getMeals'))

희망을. 내가 이렇게하면