角度TS错误TS1109 [英] Angular TS Error TS1109

查看:864
本文介绍了角度TS错误TS1109的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制台中收到以下错误:

i get following error in console:

webpack:正在编译... 10%构建模块0/1模块1处于活动状态 ... p/shoppinglist2/src/app/app.module.ts中的错误 src/app/meal.service.ts(46,56):错误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.

日期:2017-12-31T09:55:50.224Z
哈希:8003d6d8f334085afa7f时间:267ms块{inline} inline.bundle.js (内联)5.79 kB [条目]块{main} main.bundle.js(main)73.1 kB [初始] [渲染]块{polyfills} polyfills.bundle.js(polyfills) 558 kB [初始]块{styles} styles.bundle.js(styles)456 kB [初始]块{供应商} vendor.bundle.js(供应商)11.3 MB [初始]

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:编译成功.

webpack: Compiled successfully.

我看不到问题.该代码基于 https://angular.io/tutorial/toh-pt6 . 我正在使用Angular ^ 5.0.0,rxjs ^ 5.5.2

I don't see the problem. The code is based on the Angular HTTP example from https://angular.io/tutorial/toh-pt6. I'm using Angular ^5.0.0, rxjs ^5.5.2

feat.service.ts中的代码:

Code from 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 -

src/app/meal.service.ts(46,56)中的

ERROR:错误TS1109:表达式 预期

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

这篇关于角度TS错误TS1109的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆