![](/img/trans.png)
[英]The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
[英]The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead
當我嘗試初始化“this.post”以分配給變量時,它不允許我這樣做。 我正在使用 Angular 8
import { Component, OnInit } from '@angular/core';
import { HttpClientModule, HttpClient, HttpBackend } from '@angular/common/http';
import { Observable, observable } from 'rxjs';
@Component({
selector: 'app-post',
templateUrl: './post.component.html',
styleUrls: ['./post.component.css']
})
export class PostComponent implements OnInit {
post: any[];
constructor(private http: HttpClient) {
http.get('https://jsonplaceholder.typicode.com/posts').subscribe(responce => {
this.post = responce
})
}
ngOnInit() {
}
}
這里 TypeScript 抱怨類型不匹配,訂閱 function 參數的類型為 object 並且您將其分配給其他類型
httpClient.get function 是通用的意味着你可以告訴它什么是響應類型,如果你不提供它,它將默認設置為 object。
好像您正在將您的回復分配給任何類型的帖子
所以這會起作用
constructor(private http: HttpClient) {
http.get<any[]>('https://jsonplaceholder.typicode.com/posts').subscribe(responce => {
this.post = responce
})
}
這里 typescript 抱怨類型不匹配響應是 object 並且您將其分配給其他類型
httpClinet.get function 是通用的,意味着你可以告訴它什么是響應類型,如果你不提供它,它將默認設置為 object
好像您正在將您的回復分配給任何類型的帖子
所以這會起作用
constructor(private http: HttpClient) {
http.get<any[]>('https://jsonplaceholder.typicode.com/posts').subscribe(responce => {
this.post = responce
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.