Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Angular中直接将HTTP响应分配给接口模型类型_Angular_Typescript_Angular5 - Fatal编程技术网

如何在Angular中直接将HTTP响应分配给接口模型类型

如何在Angular中直接将HTTP响应分配给接口模型类型,angular,typescript,angular5,Angular,Typescript,Angular5,应用程序组件.ts import { Component,OnInit } from '@angular/core'; import { DataServiceService } from './data-service.service'; import {IModel} from './imodel'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: [ './app.

应用程序组件.ts

import { Component,OnInit } from '@angular/core';
import { DataServiceService } from './data-service.service';
import {IModel} from './imodel';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular 5';
  resultArray:IModel;
  constructor(private dataService:DataServiceService){}
  ngOnInit(){
    this.dataService.getDataFromURL().subscribe((resultData)=>{
      debugger
      this.resultArray=resultData;
    })
  }
}
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class DataServiceService {

  apiURL='https://jsonplaceholder.typicode.com/posts';
  constructor(private http:HttpClient) { }

  getDataFromURL(){
    return this.http.get(this.apiURL)
  }


}
export interface IModel {
       userId: number;
        id: number;
        title: string;
        body: string;
}
dataservice.service.ts

import { Component,OnInit } from '@angular/core';
import { DataServiceService } from './data-service.service';
import {IModel} from './imodel';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular 5';
  resultArray:IModel;
  constructor(private dataService:DataServiceService){}
  ngOnInit(){
    this.dataService.getDataFromURL().subscribe((resultData)=>{
      debugger
      this.resultArray=resultData;
    })
  }
}
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class DataServiceService {

  apiURL='https://jsonplaceholder.typicode.com/posts';
  constructor(private http:HttpClient) { }

  getDataFromURL(){
    return this.http.get(this.apiURL)
  }


}
export interface IModel {
       userId: number;
        id: number;
        title: string;
        body: string;
}
imodel.ts

import { Component,OnInit } from '@angular/core';
import { DataServiceService } from './data-service.service';
import {IModel} from './imodel';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular 5';
  resultArray:IModel;
  constructor(private dataService:DataServiceService){}
  ngOnInit(){
    this.dataService.getDataFromURL().subscribe((resultData)=>{
      debugger
      this.resultArray=resultData;
    })
  }
}
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class DataServiceService {

  apiURL='https://jsonplaceholder.typicode.com/posts';
  constructor(private http:HttpClient) { }

  getDataFromURL(){
    return this.http.get(this.apiURL)
  }


}
export interface IModel {
       userId: number;
        id: number;
        title: string;
        body: string;
}
我创建了一个模型接口,并尝试从API获取数据。 我已成功检索数据,但无法将响应分配给resultArray,因为它是IModel类型,这表明错误“type‘Object’不可分配给type‘IModel’。 “Object”类型可分配给其他很少的类型。您是否打算改用“any”类型?
类型“Object”中缺少属性“userId”。

http.get方法是泛型的。这意味着您可以像这样指定返回类型:

getDataFromURL(): Observable<IModel> {
  return this.http.get<IModel>(this.apiURL)
}
getDataFromURL():可观察{
返回this.http.get(this.apirl)
}

阅读有关typescript的更多信息。

我不同意这个答案。它没有分配给模型中的值。如果在获得相同的结果后尝试更改或删除模型中的一个属性,则不会引发异常。示例:获取数据后删除
body:string;
。不会引发异常。这是错误的