Ошибка при создании объекта из interface
Разрабатывал приложение на Angular и получил ошибку
"Property 'post' has no initializer and is not definitely assigned in the constructor."
подскажите, как ее исправить.
app.components.ts
import { Component } from '@angular/core';
export interface Post {
title: string;
text: string;
id?: number;
}
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent {
posts: Post[] = [
{ title: 'Wanna lear Angular...', text: 'Im learning smth', id: 1 },
{ title: 'lorem2', text: 'lksdnlfsd', id: 2 },
];
}
post.component.ts
import { Component, Input, OnInit } from '@angular/core';
import { Post } from '../app.component';
@Component({
selector: 'app-post',
templateUrl: './post.component.html',
styleUrls: ['./post.component.scss'],
})
export class PostComponent implements OnInit {
@Input() post: Post;
constructor() {}
ngOnInit(): void {}
}
Ответы (1 шт):
Автор решения: NocteFury
→ Ссылка
Добавь восклицательный знак сразу после слова post: @Input() post!: Post;.
Таким образом, ты указываешь TypeScript, что данное поле проинициализировано (извне, например через декоратор).