首页 > 解决方案 > 如何从Angular中的组件初始化包含其他接口的接口

问题描述

我正在使用返回 json 的服务调用 firebase,我在接口中对其进行了建模以便能够访问其数据,接口如下所示:

export interface Restaurante {
  footer: Footer;
  logo: string;
  section_1: Section1;
  section_2: Section2;
  slide_header: Slide[];
}

export interface Footer {
  contacto: Contacto;
  logo_footer: string;
  redes: Redes;
}

export interface Contacto {
  direccion: string;
  mail: string;
  telefono: string;
}

export interface Redes {
  facebook: string;
  instagram: string;
}

export interface Section1 {
  slide_promo: Slide[];
}

export interface Slide {
  img: string;
}

export interface Section2 {
  type_menu: TypeMenu[];
}

export interface TypeMenu {
  menu: Menu[];
  type: string;
}

export interface Menu {
  description: string;
  img: string;
  price: string;
  title: string;
}

我设法访问了完全初始化为联系人界面的界面,但是当我必须访问来自另一个界面内的页脚并又包含其他界面的数据时,就会出现问题。

datos: Contacto = {
    direccion: 'string',
    mail: 'string',
    telefono: 'string',
  }; 
  constructor(private firebase: FirebaseService) {
    this.firebase.datosHeader().subscribe(
      (datos) => {
        this.datos = datos.footer.contacto;
      },
      (err) => {}
    );
    console.log(this.datos);
  }

当我按照以下方式执行此操作时,它会给我一个错误,所以我想知道如何访问页脚中的信息。

datos!: Footer;
  /* datos: Contacto = {
    direccion: 'string',
    mail: 'string',
    telefono: 'string',
  };  */
  constructor(private firebase: FirebaseService) {
    this.firebase.datosHeader().subscribe(
      (datos) => {
        this.datos = datos.footer;
      },
      (err) => {}
    );
    console.log(this.datos);
  }

标签: angulartypescript

解决方案


推荐阅读