简体   繁体   English

角度TS错误TS1109

[英]Angular TS Error TS1109

i get following error in console: 我在控制台中收到以下错误:

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. webpack:编译... 10%构建模块0/1模块1活动... p / shoppinglist2 / src / app / app.module.tsERROR在src / app / meal.service.ts(46,56):错误TS1109 :表达预期。

Date: 2017-12-31T09:55:50.224Z 日期: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] 哈希:8003d6d8f334085afa7f时间:267ms chunk {inline} inline.bundle.js(内联)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. webpack:编译成功。

I don't see the problem. 我没有看到问题。 The code is based on the Angular HTTP example from https://angular.io/tutorial/toh-pt6 . 该代码基于https://angular.io/tutorial/toh-pt6中的Angular HTTP示例。 I'm using Angular ^5.0.0, rxjs ^5.5.2 我正在使用Angular ^ 5.0.0,rxjs ^ 5.5.2

Code from meal.service.ts: 代码来自meal.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>))
      );
  }
}

You are calling generic method in wrong way so that you get this - 你以错误的方式调用泛型方法,这样你就得到了 -

ERROR in src/app/meal.service.ts(46,56): error TS1109: Expression expected src / app / meal.service.ts中的错误(46,56):错误TS1109:预期的表达式

Wrong way 错误的方法

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

Correct way 正确的方式

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

if you check the handleError function in the documentation that you refered to, you will notice it's expecting to receive a parameter, 如果你检查你提到的文档中的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);
 };
}

But in your code, you are using the handleError function wrong. 但是在您的代码中,您使用的是handleError函数错误。

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

That's why you get this - ERROR in src/app/meal.service.ts(46,56): error TS1109: Expression expected. 这就是为什么你得到这个 - 在src / app / meal.service.ts(46,56)中的错误:错误TS1109:预期的表达式。 So try this - 试试这个 -

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

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM