要在Angular 7中获取JSON请求中特定的子对象,可以使用HttpClient模块来发送HTTP请求并处理响应。
以下是一个示例代码,演示如何获取JSON请求中特定的子对象:
HttpClientModule模块。在你的app.module.ts文件中,添加以下代码:import { HttpClientModule } from '@angular/common/http';
@NgModule({
  imports: [
    HttpClientModule
  ],
  // ...
})
export class AppModule { }
HttpClient模块,并注入到构造函数中:import { HttpClient } from '@angular/common/http';
export class YourComponent {
  constructor(private http: HttpClient) { }
  // ...
}
http.get()方法发送HTTP GET请求,并使用subscribe()方法订阅响应:export class YourComponent {
  constructor(private http: HttpClient) { }
  getSpecificObject() {
    this.http.get('your-api-url').subscribe(data => {
      const specificObject = data.subObject; // 通过指定的属性名获取子对象
      console.log(specificObject);
    });
  }
}
 
在上面的示例中,your-api-url是你要发送请求的API的URL。你可以将其替换为实际的API URL。
getSpecificObject()方法来获取特定的子对象:export class YourComponent {
  // ...
  
  ngOnInit() {
    this.getSpecificObject();
  }
}
上述代码将在组件初始化时调用getSpecificObject()方法,并获取特定的子对象。
请注意,上述示例假设你的API返回的数据是一个JSON对象,并且包含一个名为subObject的属性,该属性代表你想要获取的特定子对象。你可以根据你的实际需求调整代码来获取不同的子对象。
                    上一篇:Angular 7:获取本地千位分隔符和小数点标记字符
                
下一篇:Angular 7:获取“AppComponent cannot be used as an entry component”错误