【问题标题】:error TS1128: Declaration or statement expected for .map() operator错误 TS1128:.map() 运算符需要声明或语句
【发布时间】:2018-08-09 13:37:43
【问题描述】:

尝试了来自不同站点的几种解决方案,但没有帮助。 问题是:尝试运行我的应用程序但出现以下错误:


10% 构建模块 0/1 模块 1 处于活动状态…\src\app\contacts\contacts.component.tsERROR in src/app/contact.service.ts(18,7): error TS1128: Declaration or statement expected. src/app/contact.service.ts(27,7):错误 TS1128:需要声明或声明。 src/app/contact.service.ts(33,7):错误 TS1128:需要声明或声明。


    import { Injectable } from '@angular/core';
    import { Http, Headers, Response} from '@angular/http';
    import { Contact } from './contact';
    import 'rxjs/add/operator/map';
    import { map } from 'rxjs/operators';

            @Injectable({
      providedIn: 'root'
    })
    export class ContactService {

        constructor(private http: Http) { }

        // Retrieving contacts
        getContacts() {
          return this.http.get('http://localhost:3000/api/contacts');
          .map((res: any) => res.json());

        }

        // Adding contacts
        addContact(newContact) {
          var headers = new Headers;
          headers.append('Content-Type', 'application/json');
          return this.http.post('http://localhost:3000/api/contact', newContact, {headers: headers});
          .map((res: Response) => res.json());
        }

        // Delete contact method
        deleteContact(id) {
          return this.http.delete('http://localhost:3000/api/contact' + id);
          .map((res: Response) => res.json());
        }
    }

.map((res: any) => res.json()); 行说:

需要声明或声明。

【问题讨论】:

  • 为什么;.map 之前?应该是return this.http.get('http://localhost:3000/api/contacts').map((res: any) => res.json());
  • 我认为 .map() 应该是 .then() (?)
  • @Titian Cernicova-Dragomir : 删除 ';'解决了我的问题。谢谢。

标签: angularjs typescript


【解决方案1】:

您显然在.map 之前有;。应该是

return this.http.get('http://localhost:3000/api/contacts')
     .map((res: any) => res.json()); 

因为这会导致语句结束,.map 会出现语法错误

【讨论】:

  • 是的';' (分号)是问题所在。删除';'解决了我的问题。感谢您的帮助。
猜你喜欢
  • 2022-01-23
  • 2020-03-22
  • 1970-01-01
  • 1970-01-01
  • 2021-02-09
  • 2022-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多