如何使用Angular Observable从JSON文件中读取数据

huangapple go评论58阅读模式
英文:

How to read data from JOSN file with Angular Observable

问题

I have a JSON file called reportConfig. I want to read that data using Angular and TypeScript.

Here I have added my JSON file:

{
  "accessToken": "12345",
  "style": "color",
  "projection": "globe",
  "zoomLevel": 5,
  "markerScale": 1.1
}

and created a new class called ConfigModel:

export class MapReportConfigModel {
  accessToken: string;
  style: string;
  projection: string;
  zoomLevel: number;
  markerScale: number;

  constructor(private httpClient: HttpClient) {}

  // Read JSON data
  getMapConfig(): Observable<ReportConfigModel> {
    let configData = `assets/reportConfig.json`;
    return this.httpClient.get<ReportConfigModel>(configData);
  }
}

Now I have a child component called ReportContainer. So my question is how can I call the getMapConfig() method from the child component:

export class ReportContainerComponent implements OnInit {
  
  constructor(
    private httpClient: HttpClient,
    private mapReportConfigModel: MapReportConfigModel
  ) {}

  ngOnInit(): void {}

  // Load the initial map
  loadMap(): void {
    // Call the getMapConfig method
    this.mapReportConfigModel.getMapConfig().subscribe(data => {
      // Handle the JSON data here
      console.log(data);
    });
  }
}

I have tried this but don't know how to implement it correctly. Could someone help me to resolve this? From the child component, I want to call this method to get JSON data.

英文:

I have a JSON file called reportConfig. I want read that data using angular and typescript.

Here I have added my JSON file

{
  &quot;accessToken&quot;: &quot;12345&quot;,
  &quot;style&quot;: &quot;color&quot;,
  &quot;projection&quot;: &quot;globe&quot;,
  &quot;zoomLevel&quot;: 5
  &quot;markerScale&quot;: 1.1
}

and created new class called ConfigModel

export class MapReportConfigModel {
  accesToken: string;
  style: string;
  projection: number;
  zoomLevel: number;
  markerScale: number;

  constructor(private httpClient: HttpClient) {}

  // read json data
  getMapConfig(): Observable&lt;ReportConfigModel &gt; {
    let configData = `assets/reportConfig.json`;
    return this.httpClient.get&lt;ReportConfigModel&gt;(configData );
  }
}

Now I have child component called ReportContainer. so my question is how can I call from child component to getMapConfig() method.

export class ReportContainerComponent implements OnInit {
  
  constructor(
    private httpClient: HttpClient,
    private mapReportConfigModel: MapReportConfigModel
  ) {}

  ngOnInit(): void {}

  //load initial map
  loadMap(): void {

  }

  private getMapConfigData(): void {
    this.httpClient.get(this.mapReportConfigModel);
  }
}

I have tried this but don't know how to implement in correctly.

Could someone can help me to resolve this. from child component I want to call this method to get json data.

答案1

得分: 0

所以我的问题是如何从子组件调用 getMapConfig() 方法。

订阅从 getMapConfig 方法获取的 Observable,它位于 mapReportConfigModel 私有变量上。

export class ReportContainerComponent implements OnInit {
  
  constructor(
    private mapReportConfigModel: MapReportConfigModel
  ) {}

  ngOnInit(): void {
    // 从你的组件中调用依赖
    this.mapReportConfigModel.getMapConfig().subscribe((result) => {
       console.log(result);
    });
  }
}
英文:

> so my question is how can I call from child component to
> getMapConfig() method.

Subscribe to the Observable that you get from your getMapConfig method on the mapReportConfigModel private variable.

export class ReportContainerComponent implements OnInit {
  
  constructor(
    private mapReportConfigModel: MapReportConfigModel
  ) {}

  ngOnInit(): void {
    // Invoke the dependency from your component
    this.mapReportConfigModel.getMapConfig().subscribe((result) =&gt; {
       console.log(result);
    });
  }
}

huangapple
  • 本文由 发表于 2023年3月3日 20:56:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定