简体   繁体   English

Angular / TypeScript:如何解决此编译问题?

[英]Angular/TypeScript : How to resolve this compilation issue?

Hell I ma new in angular 5. I am create a login and auth service. 地狱我是角5的新人。我正在创建登录和身份验证服务。 But i cannot compile my code. 但我无法编译我的代码。 Here is my code 这是我的代码

// user.service.ts
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

@Injectable()
export class UserService {
  private loggedIn = false;

  constructor(private http: Http) {
  //  this.loggedIn = !!localStorage.getItem('auth_token');
  }

//authenticate user with dummy logic because i use a json-server
  authenticate(login:string, password:string) {
    console.log('Authenticate ....');
    const credentials = {login:login, password:password};
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');

    var result = this.http
      .get(
        '/users?login?'+JSON.stringify(login),
        { headers }
      );

      if(result.password==password){
        return true;
      }
      return false;
    }


}

When i compile ( ng server ) i get the following error 当我编译(ng服务器)时,我收到以下错误

ERROR in src/app/auth/user.services.ts(28,17): error TS2339: 
Property 'password' does not exist on type 'Observable<Response>'.

Line 28 is : if(result.password==password){ 第28行是: if(result.password==password){

I don't know what i am missing ?I try to understand the Observable concept. 我不知道我错过了什么?我试着理解Observable的概念。 If you add an idea, it will help me. 如果你添加一个想法,它将帮助我。 Thanks 谢谢

result here is an observable, you need to subscribe to it to get response. result这里是一个可观察的,你需要订阅它以获得响应。

Something like below: 如下所示:

     var result = this.http.get(
                   '/users?login?'+JSON.stringify(login),
                    { headers }
     ); 
     //result is an observer here, you have to subscribe to it
     result.subscribe((response) =>  {
           if(response.password==password){
                     return true;
           }
           return false;
      });

You can check this awesome article: https://gist.github.com/staltz/868e7e9bc2a7b8c1f754 你可以查看这篇很棒的文章: https//gist.github.com/staltz/868e7e9bc2a7b8c1f754

  1. Use Observables properly 正确使用Observables
  2. Use HttpClient , not old Http 使用HttpClient ,而不是旧的Http
  3. You can also define a User class to make typing more strict. 您还可以定义User类以使输入更严格。

  // user.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

@Injectable()
export class UserService {
  private loggedIn = false;

  constructor(private http: HttpClient) {
  //  this.loggedIn = !!localStorage.getItem('auth_token');
  }

  //authenticate user with dummy logic because i use a json-server
  authenticate(login:string, password:string) :Observable<boolean> {
    return this.http
      .get('url/whatever') //returns a User object having password
      .map(user => user.password === password); // maps result to the desired true or false value
  }
}

// to consume the service from a component, for example

this.userService.authenticate('myusername', 'mypassword')
    .subscribe(authenticated => {
        console.log('login status', authenticated)
    })

You are trying to access the observable returned from the http call. 您正在尝试访问从http调用返回的observable。 To get the information in the observable you have to subscribe to it. 要获得可观察信息,您必须订阅它。

For detailed information about hot to get remote data please read this: https://angular.io/guide/http 有关热获取远程数据的详细信息,请阅读: https//angular.io/guide/http

NOTE: You should not use the deprecated angular/http. 注意:您不应使用已弃用的angular / http。 Use angular/common/http instead. 请改用angular / common / http。

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

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