【问题标题】:getting a object with HttpClient angular 4 and call it a component使用 HttpClient angular 4 获取对象并将其称为组件
【发布时间】:2018-04-21 12:28:52
【问题描述】:

我没有练习并试图复习 Angular。我想返回一个对象并将其呈现在 HTML 文件中。

服务:

@Injectable()
export class NameService {

    url:string = "";

  constructor(private http: HttpClient) { }


    public getActivities(): Observable<UserModel> {
        return this.http.get(this.url);
    }


}

组件:

export class NameComponent implements OnInit {

    users:UserModel;

  constructor(private nameService: NameService) { }

  public getUsers(){
      this.users = this.nameService.getActivities()
          .subscribe(res => {this.users = res});
  }

  ngOnInit() {
      this.getUsers();
  }

错误Type 'Subscription' is not assignable to type 'UserModel'.

Type 'Observable&lt;Object&gt;' is not assignable to type 'Observable&lt;UserModel&gt;'.

HTML:

<ul>
        <li *ngFor="let user of users; let i = index">
            {{i + 1}} - {{user.id}}
            {{i + 1}} - {{user.first_name}}
            {{i + 1}} - {{user.last_name}}
            {{i + 1}} - {{user.email}}
            {{i + 1}} - {{user.ip}}
        </li>
    </ul>

和 JSON:

{
    "id": 11,
    "first_name": "Blakelee",
    "last_name": "Gillingham",
    "email": "bgillinghama@engadget.com",
    "gender": "Female",
    "ip_address": "234.175.225.8"
}, {
    "id": 12,
    "first_name": "Tally",
    "last_name": "Loraine",
    "email": "tloraineb@marriott.com",
    "gender": "Female",
    "ip_address": "50.20.33.244"
},

我正在使用 Angular 5,但使用 Angular 4 实践,因为这样做会破坏我的其他应用程序。

-----------------更新 1-------------------

我加了users:UserModel[]=[];

然后我添加了

public getActivities(): Observable<UserModel[]> {
        return this.http.get<UserModel[]>(this.url);
    }

ide 停止抱怨,但现在我又回到了错误:

No provider for HttpHandler!

------------------编辑 2------------------------

【问题讨论】:

  • 嘿伙计,如果(或者)有帮助,介意标记答案吗? ;)

标签: angular


【解决方案1】:

问题是,HttpClient 返回的是Object,因此错误消息是正确的。你想告诉 Angular 你的响应是UserModel 类型,而不是Object 类型。除此之外,您会得到一个UserModel 的数组(我认为这是您的问题中的一个错字)。所以你的请求应该是这样的:

public getActivities(): Observable<UserModel[]> {
    return this.http.get<UserModel[]>(this.url);
}

此外,您的订阅组件看起来就像 Sajeetharan 指出的那样:

public getUsers(){
  this.nameService.getActivities()
    .subscribe(res => this.users = res);
}

编辑:并在组件中声明您的 users

users:UserModel[] = [];

阅读更多关于(不是那么新的)HttpClient 和类型检查的信息:https://angular.io/guide/http#typechecking-the-response

编辑 2:

至于后续错误,请确保您已导入正确的模块(HttpClientModule)并在BrowserModule之后声明它:Error: No provider for HttpHandler in angular2

【讨论】:

  • 我这样做了,现在它显示No provider for HttpHandler!,我也将它添加到模块中。感谢您的帮助。猫在哪里?
  • ` 类型 'UserModel[]' 中缺少属性 'id'。` 并且类型 'Observable' 不能分配给类型 'Observable
  • 您仍然遇到打字问题,请注意您已在所有地方将传入数据标记为&lt;UserModel[]&gt;。另一个标记为UserModel 类型的对象。我的小猫很好,谢谢 :D 睡在某个地方,为他们的夜间冒险做准备(世界这边很快就到了晚上)
  • 您将组件变量users 声明为对象,将其标记为users:UserModel[];(更新答案)
  • 您是否也更新了服务?在返回类型和获取请求上都声明了&lt;UserModel[]&gt;?如果您的所有类型都匹配,这应该可以工作并且不会吐出此类错误。
【解决方案2】:

如下修改,

export class NameComponent implements OnInit {

    users:UserModel[]=[];

  constructor(private nameService: NameService) { }

  public getUsers(){
     this.nameService.getActivities()
          .subscribe(res => {this.users = res});
  }

  ngOnInit() {
      this.getUsers();
  }

【讨论】:

  • Type 'Observable&lt;Object&gt;' is not assignable to type 'Observable&lt;UserModel&gt;'. 它说Did you mean to use the 'any' type instead? Property 'id' is missing in type 'Object'.
  • 更新后的答案怎么样
  • `类型“UserModel”不可分配给类型“UserModel[]”。`
  • 所以它不是一个数组,试试 users:UserModel=[];
  • 你错过了什么
猜你喜欢
  • 2017-12-15
  • 1970-01-01
  • 2017-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多