简体   繁体   English

Typescript 找不到名称“地图”

[英]Typescript cannot find name 'map'

I'm new to Angularjs. I need to send post request to my localhost server.我是 Angularjs 的新手。我需要向我的本地主机服务器发送发布请求。 But I got an error [ts] cannot find name 'map' and cannot find name 'subscribe'.但是我得到一个错误 [ts] 找不到名称“地图”并且找不到名称“订阅”。 i hope will understand my error.我希望能理解我的错误。 Thanks in advance.提前致谢。

login.ts登录.ts

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Http ,Headers ,HttpModule} from '@angular/http';
import 'rxjs/add/operator/map';

/**
 * Generated class for the Login page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class Login {

  constructor(public navCtrl: NavController, public navParams: NavParams,public http:Http) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad Login');
  }
  ionViewidData(){
    let headers= new Headers();
    headers.append('Content-Type','application/json');

    let body = {
      device:"Mobile",
      id:"10077",
      password:"leechan2"
    };
    console.log(body);
    this.http.post('http://0.0.0.0:2800/login',JSON.stringify(body),{headers:headers});
        .map(res => res.json())
        .subscribe(data =>{
          console.log(data);
        });

}

}

you have an unexpected ; 你出乎意料; at line this.http.post . this.http.post行。

this.http.post('http://0.0.0.0:2800/login',JSON.stringify(body),{headers:headers})           //    <--------- remove ; here
    .map(res => res.json())
    .subscribe(data =>{
      console.log(data);
    });

**Remove ; **去掉 ; after this.http.post and try to use private http:Http in constructor ** 在this.http.post之后,尝试在构造函数中使用私有http:Http

this.http.post('http://0.0.0.0:2800/login',JSON.stringify(body),{headers:headers})
    .map(res => res.json())
    .subscribe(data =>{
      console.log(data);
    });

You should import it before using:您应该在使用之前导入它:

import { map } from 'rxjs/operators';

This works for me.这对我有用。

Syntax error can sometimes give strange errors which might not point directly to your root cause. 语法错误有时会带来奇怪的错误,这些错误可能不会直接指向您的根本原因。 You finished your 'post' method with a ';' 您使用“;”结束了“发布”方法 so your 'map' method is not called on any object. 因此您的“地图”方法不会在任何对象上调用。 Nothing on the left side of the '.' '。'的左侧没有内容。 is causing the 'map' method not to be found. 导致找不到“地图”方法。 Remove the ';' 去除 ';' after 'headers});'. 在“标题”之后;”。

Like this: this.http.post('http://0.0.0.0:2800/login',JSON.stringify(body),{headers:headers}).map(res => res.json()).subscribe(data =>{ console.log(data); }); 像这样: this.http.post('http://0.0.0.0:2800/login',JSON.stringify(body),{headers:headers}).map(res => res.json()).subscribe(data =>{ console.log(data); });

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

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