获取空的JSON数据

时间:2018-10-01 07:56:00

标签: javascript json angular angular6

我正在尝试从url中获取json数据,但是我却无法获取任何数据。    它只是显示一个空数组。你觉得我想念什么    在这里?

service.ts

这是service.ts。我试图从“ https:// jsonplace ”获取数据。

 从“ @ angular / core”导入{可注入};
从“ ../../models/user-creation.model”导入{UserCreation};
从“ rxjs / Observable”导入{Observable};
从“ rxjs / observable / of”导入{of};
从'rxjs / operators'导入{catchError,map,tap};
从“ @ angular / common / http”导入{HttpClient,HttpHeaders};

const httpOptions = {
    headers:new HttpHeaders({'Content-Type':'application / json'})
};

@Injectable({
providerIn:'root'
})

导出类UserCreationService {

//创建构造函数以获取Http实例
构造函数(私有http:HttpClient){}

私人用户网址:'https://jsonplaceholder.typicode.com/posts';

getUsers():Observable  {

返回this.http.get (this.usersUrl).pipe(
点击(receivedUsers
=> console.log(`receivedUsers = $ {JSON.stringify(receivedUsers)})),
catchError(错误=>(([]))
);
}
 

app.component.ts

这是component.ts文件

 从'@ angular / core'导入{Component,OnInit};
从'@ angular / http'导入{Http,Response};
从'../../common/services/user-导入{UserCreationService}
creation.service';


@零件({
选择器:“ app”,
templateUrl:“ ./ app.component.html”,
styleUrls:['./ app-component.css']
})

导出类AppComponent实现OnInit {

allUsers:UserCreation [];

构造函数(私有userService:UserCreationService){}

getUsersFromServices():void {
this.userService.getUsers()。subscribe(
  (用户)=> {
    this.allUsers =用户;
    console.log(`this.allUsers = $ {JSON.stringify(this.allUsers)}`);

  }
 )
}

ngOnInit():void {
this.getUsersFromServices();
}
 

1 个答案:

答案 0 :(得分:2)

这里有拼写错误-#!/bin/sh am_i_root() { [ "$(id -u)" -eq 0 ] } if am_i_root; then tput bold; tput setaf 2; echo 'Ok, you are root!'; tput sgr0 else tput bold; tput setaf 1; echo 'Sad, you are NOT root :('; tput sgr0 fi

应该是private usersUrl:'https://jsonplaceholder.typicode.com/posts';而不是=-:

或者更好的方法private usersUrl='https://jsonplaceholder.typicode.com/posts';