Angular http客户端错误[重复]

问题描述 投票:1回答:1

这是我的应用程序的一个组件,它工作得很好但它在this.stylistDetails = data;this行中出错。有人可以建议解决它的方法

import { Component, OnInit } from '@angular/core';
    import {ActivatedRoute} from '@angular/router';
    import {HttpClient} from '@angular/common/http';

    @Component({
      selector: 'app-search-results',
      templateUrl: './search-results.component.html',
      styleUrls: ['./search-results.component.css']
    })
    export class SearchResultsComponent implements OnInit {

      stylistDetails: Stylist[] = [];
      need;
      type;
      showMessage;
      preferred_locations = [];

      countResults;

      // onNotifyClicked(message:string): void{
      //   this.showMessage = message;
      // }

      onClickLocFiltered(): void{
        this.http.get('/api/stylist/getStylist/loc').subscribe(
          data => {
            this.stylistDetails = data;
            this.countResults = this.stylistDetails.length;
          }
        );
      }

      constructor(private  route: ActivatedRoute, private http: HttpClient) {

        this.http.get<Stylist>('/api/stylist/getStylistName').subscribe(
          data => {
            this.stylistDetails = data;
            // console.log(this.stylistName);
            // console.log(this.stylistName.length);
            this.countResults = this.stylistDetails.length;
          }
        );


      }

      ngOnInit() {
        this.route
          .queryParams
          .subscribe(params => {
            // Defaults to 0 if no query param provided.
            this.need = params['need'] || 0;
            this.type = params['type'] || 0;
          });
      }
    }

    interface Stylist {
      name: string;
      address: string;
      cost: number;
      skills: string[];
    }

这是我的终端显示的错误

ERROR in src/app/search-results/search-results.component.ts(27,9): error TS2322: Type 'Object' is not assignabl
e to type 'Stylist[]'.
  The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
src/app/search-results/search-results.component.ts(27,9): error TS2322: Type 'Object' is not assignable to type
 'Stylist[]'.
  The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
    Property 'includes' is missing in type 'Object'.
src/app/search-results/search-results.component.ts(37,9): error TS2322: Type 'Stylist' is not assignable to typ
e 'Stylist[]'.
src/app/search-results/search-results.component.ts(37,9): error TS2322: Type 'Stylist' is not assignable to typ
e 'Stylist[]'.
  Property 'includes' is missing in type 'Stylist'.
angular express webstorm
1个回答
1
投票

将您的订阅方法从构造函数更改为:

this.http.get<any>('/api/stylist/getStylistName').subscribe(
  data => {
    this.stylistDetails = data;
    // console.log(this.stylistName);
    // console.log(this.stylistName.length);
    this.countResults = this.stylistDetails.length;
  }
);
© www.soinside.com 2019 - 2024. All rights reserved.