首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角TS误差TS1109

角TS误差TS1109
EN

Stack Overflow用户
提问于 2017-12-31 10:27:39
回答 2查看 1.5K关注 0票数 3

控制台中出现以下错误:

webpack:汇编..。在src/app/meal.service.ts(46,56)中构建10%的模块0/1模块1 active .p/src/app/app.module.tsERROR(46,56):错误TS1109:预期表达式。 日期: 2017-12-31T09:55:50.224Z 散列: 8003d6d8f334085afa7f时间:267 MB块{内联} inline.bundle.js (内联) 5.79 kB条目块{main} main.bundle.js (main) 73.1 kB初始块{inline} polyfills.bundle.js (inline) 558 kB初始块{styles} styles.bundle.js (styles) 456 kB初始块{供应商} vendor.bundle.js (供应商) 11.3 MB初始 webpack:编译成功。

我看不出问题所在。代码基于来自https://angular.io/tutorial/toh-pt6的示例。我用的是角^5.0.0,rxjs ^5.5.2

来自meal.service.ts的代码:

代码语言:javascript
复制
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>))
      );
  }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-12-31 10:48:56

你用错误的方式调用了通用方法,所以你得到了-

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

错路

代码语言:javascript
复制
catchError(this.handleError('getMeals', <Meals>)) // Line 46: the Error

校正方式

代码语言:javascript
复制
catchError(this.handleError<Meals>('getMeals'))
代码语言:javascript
复制
 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
      );
  };
票数 5
EN

Stack Overflow用户

发布于 2017-12-31 10:59:37

如果您在所引用的文档中检查handleError函数,您会注意到它希望收到一个参数,

代码语言:javascript
复制
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函数是错误的。

代码语言:javascript
复制
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
      );
  };

这就是为什么在src/app/meal.service.ts(46,56)中出现了-错误: TS1109: Expression。

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

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48041698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档