错误TypeError:无法读取未定义的属性'getUsers'

时间:2018-11-11 10:16:40

标签: angular hibernate spring-boot

listuser.component.ts

import { Component, OnInit } from '@angular/core';
import { User } from 'src/app/user';
import { UserService } from 'src/app/shared_service/user.service';

@Component({
  selector: 'app-listuser',
  templateUrl: './listuser.component.html',
  styleUrls: ['./listuser.component.css']
})
export class ListuserComponent implements OnInit {
  private users:User[];
  _userService: any;
  constructor(private_userService:UserService) { }

  ngOnInit() {
    this._userService.getUsers().subscribe((users)=>{
      console.log(users);
      this.users=users;
    },(error)=>{
      console.log(error);
    })
  }

}

user.service.ts

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions} from '@angular/http';
import { Observable } from 'rxjs';
import  'rxjs/add/operator/map';
import 'rxjs-compat';
import 'rxjs/add/operator/catch';
import  'rxjs/add/observable/throw';

import { User } from '../user';

@Injectable({
  providedIn: 'root'
})
export class UserService {
  private baseUrl:string="http://localhost:8080/api";
  private headers = new Headers({'Content-Type':'application/json'});
  private options = new RequestOptions({headers:this.headers});
  _http: any;
  constructor(private_http:Http) { }

  getUsers()
  {
    return this._http.get(this.baseUrl+'/users',this.options).map((response:Response)=>response.json())
    .catch(this.errorHandler);

  }
  getUser(id:Number)
  {
    return this._http.get(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
    .catch(this.errorHandler);

  }
  deleteUser(id:Number)
  {
    return this._http.delete(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
    .catch(this.errorHandler);

  }
  createUsers(user:User)
  {
    return this._http.post(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
    .catch(this.errorHandler);

  }
  updateUsers(user:User)
  {
    return this._http.put(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
    .catch(this.errorHandler);

  }
  errorHandler(error:Response){
    return Observable.throw(error||'SERVER ERROR');
  }

}

运行此..错误后: 错误TypeError:无法读取未定义的属性'getUsers'     在ListuserComponent.push ../ src / app / components / listuser / listuser.component.ts.ListuserComponent.ngOnInit(listuser.component.ts:16)     在checkAndUpdateDirectiveInline(core.js:18620)     在checkAndUpdateNodeInline(core.js:19884)     在checkAndUpdateNode(core.js:19846)     在debugCheckAndUpdateNode(core.js:20480)     在debugCheckDirectivesFn(core.js:20440)     在Object.eval [作为updateDirectives](ListuserComponent_Host.ngfactory.js?[sm]:1)     在Object.debugUpdateDirectives [作为updateDirectives](core.js:20432)     在checkAndUpdateView(core.js:19828)     在callViewAction(core.js:20069)

3 个答案:

答案 0 :(得分:0)

好像您忘记了在构造函数中的private之后和_userService之前添加空格:

constructor(private _userService:UserService) { }

答案 1 :(得分:0)

您需要在private_userService之间添加空格,因此

constructor(private_userService:UserService) { }

应该是

constructor(private _userService: UserService) { }

答案 2 :(得分:0)

您的服务类别中有几个问题-

  1. private_http之间的空格
  2. 删除其他声明的实例变量_http: any;

修改后的代码

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions} from '@angular/http';
import { Observable } from 'rxjs';
import  'rxjs/add/operator/map';
import 'rxjs-compat';
import 'rxjs/add/operator/catch';
import  'rxjs/add/observable/throw';

import { User } from './user';

@Injectable({
  providedIn: 'root'
})
export class UserService {
  private baseUrl:string="http://localhost:8080/api";
  private headers = new Headers({'Content-Type':'application/json'});
  private options = new RequestOptions({headers:this.headers});
  constructor(private _http:Http) { }

  getUsers()
  {
    return this._http.get(this.baseUrl+'/users',this.options).map((response:Response)=>response.json())
    .catch(this.errorHandler);

  }
  getUser(id:Number)
  {
    return this._http.get(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
    .catch(this.errorHandler);

  }
  deleteUser(id:Number)
  {
    return this._http.delete(this.baseUrl+'/user/'+id,this.options).map((response:Response)=>response.json())
    .catch(this.errorHandler);

  }
  createUsers(user:User)
  {
    return this._http.post(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
    .catch(this.errorHandler);

  }
  updateUsers(user:User)
  {
    return this._http.put(this.baseUrl+'/users',JSON.stringify(user), this.options).map((response:Response)=>response.json())
    .catch(this.errorHandler);

  }
  errorHandler(error:Response){
    return Observable.throw(error||'SERVER ERROR');
  }

}