订阅不存在"订阅"类型

Sho*_*bal 6 typescript angular

我是angualr 2的新手.我正在做一个小练习.我有一个用户在登录时对用户进行身份验证的服务.这是它的登录功能.

login(email:string, password:string){

        var loginrul="https://filing.herokuapp.com/api/v1/auth/login/";
        return this.http.post(loginrul,JSON.stringify({  password: password,email: email }),
            {headers:new Headers({'Content-Type':'application/json'})}
            ).map(res=>res.json()).
            subscribe(
                data => localStorage.setItem('id_token',data.auth_token),
                error=>console.log(error)
            );
    }
Run Code Online (Sandbox Code Playgroud)

当我在组件中调用此函数时,angualr会给出"属性订阅不存在于'订阅'上的错误".

doLogin(event) {
        console.log(event);
        console.log(this.loginForm.value);


        let formData = this.loginForm.value;
        let email = this.loginForm.controls.email.value;
        let password= this.loginForm.controls.password.value;
        this.auth.login(email,password).subscribe(res =>{
            console.log(res);
            if(res.status===200){
                this.router.navigate(['dashboard']);
            }
        }
        )
    }
Run Code Online (Sandbox Code Playgroud)

我认为为什么angular会出现这个错误,因为我在登录功能中使用订阅以及在调用登录功能时.请建议一些解决方案或替代方法来实现我在登录功能中尝试实现的结果.

Fre*_*din 14

你自己对问题的分析是正确的!删除.subscribe您的服务,只在您使用它的组件中使用它.您的服务应在.map运营商之后返回.

如果您订阅了该服务,它将返回一个Subscription而不是一个Observable,这就是您收到错误的原因.