Angular http客户端错误

这是我的应用程序的一个组件,它可以很好地工作,但它会使this.stylistDetails = data;这一行出现错误。可有人建议解决这个问题Angular http客户端错误

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'.

回答:

更改您的订阅方法从构造到这个错误:

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;

}

);

以上是 Angular http客户端错误 的全部内容, 来源链接: utcz.com/qa/264826.html

回到顶部