0

Начал изучить Angular2 и встала следующая задача: я создаю компонент info, в файле info.component.ts я задаю объекты следующиего вида:

import { Comm } from '../shared/comments';
...
const VASACOM: Comm = {
  name: 'Vasa',
  id: 122,
  comments: [
    {
     rating: 5,
     comment: "Some commment",
     date: "2012-10-16T17:57:28.556094Z"
    },
    {
     rating: 4,
     comment: "Other comment",
     date: "2014-09-05T17:57:28.556094Z"
    }
  ]
};

В comments.ts класс описываю так:

export class Comm {
  name: stirng;
  id: number;
  comments: Comment[];
  constructor(name:string,id:number,comments:Comment[]){
    this.name = name;
    this.id = id;
    this.comments = comments;
  }
}

export class Comment {
  rating: number;
  comment: string;
  date: string;
}

В итоге получаю:

error TS2322: Object literal may only specify know properties, and 'comments' does not exist in type 'Comm'

Подскажите, что я сделал не так.

P.s. я понимаю, что можно создавать по отдельности 2 объекта, один класса Comm, другой Comment, но мне хочется делать это одним объектом.

1 ответ 1

1

Все, уже подсказали. Мне помогло декларирование. В файле comments.ts я описал класс следующим образом:

export class Comm {
  constructor(
     public name: string,
     public id: number,
     public comments: Comment[]
   ){}
}

export class Comment {
   constructor(
     public rating: number,
     public comment: string,
     public date: string
   ){}
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.