【问题标题】:Angular Nested Class角嵌套类
【发布时间】:2020-07-08 19:28:56
【问题描述】:

我是 Angular 的新手,但我想问有没有办法像这个 .net 类一样在 Angular 中创建嵌套类?

public class BaseResponse<T>
{
    public T Data { get; set; }
    public int StatusCode { get; set; }
    public string Message { get; set; }

    public BaseResponse()
    {

    }

    public BaseResponse(T data)
    {
        Data = data;
    }

    public BaseResponse(int statusCode, string message)
    {
        StatusCode = statusCode;
        Message = message;
    }

    public BaseResponse(T data, int statusCode, string message)
    {
        Data = data;
        StatusCode = statusCode;
        Message = message;
    }
}

当我使用这个类并将类添加到 T 对象时,例如 Section 类,这是后端的结果:

{
"Data": [
    {
        "ID": 3,
        "SectionName": "Section3",
        "Description": "Description3"
    },
    {
        "ID": 4,
        "SectionName": "Section4",
        "Description": "Description4"
    }
],
"StatusCode": 200,
"Message": "Data successfully retrieved"
}

如何从角度访问此 JSON 数据? 我尝试下面的代码但失败了。

export class Baseresponse<T> {
    Data:T;
    StatusCode:number;
    Message:string;

    constructor(data:T,statuscode:number,message:string) {
        this.Data=data;
        this.StatusCode=statuscode;
        this.Message=message;
    }
}

这是我的服务,但没有任何效果

export class SectionService {
objectResponse:Baseresponse<Section>;
 
constructor(private http:HttpClient) { }

getAllSection(){
  this.http.get(environment.wsURL+'/SectionGetAll').toPromise().then(res=>this.objectResponse=res);
  return this.objectResponse;
}
}

【问题讨论】:

    标签: javascript .net angular typescript class


    【解决方案1】:

    这是因为异步方法。当您调用 this.http.get 时,它将排队,return this.objectResponse 在那一刻返回 undefined。你需要这样做:

    export class SectionService {
      objectResponse: Baseresponse<Section>;
    
      constructor(private http:HttpClient) { }
    
      getAllSection(): Promise<Baseresponse<Section>> {
        /* 
          we return Promise object to have ability handle 
          the request or error in a place where we are going to call it 
        */
    
        return this.http
          .get(environment.wsURL + '/SectionGetAll')
          .toPromise();
    
        /* 
          if you need a simple cache you can: 
    
          if (this.objectResponse) { 
            return Promise.resolve(this.objectResponse);
          }
    
          return this.http
            .get(environment.wsURL + '/SectionGetAll')
            .toPromise()
            .then(response => this.objectResponse = response);
        */
      }
    }
    

    然后在某个组件或另一个服务中:

    export class SomeClass {
      /* inject appropriate service */
      constructor(private sectionService: SectionService) {}
    
      getAll() {
        /* fetch all sections and handle the request */
        this.sectionService.getAllSection()
          .then(response => /* handle response */)
      }
    }
    

    https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 2015-12-08
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多