【发布时间】:2020-07-18 11:20:03
【问题描述】:
我对 Angular 完全陌生,上周才开始使用它(但我有一点编程的基本背景)。我正在尝试构建一个非常基本的博客应用程序,并且我想从根(app.component.ts)获取一个值到我的组件标签“app-post-list-item”,位于我的其他组件中模板(post-list-component.html),以便我可以在我的帖子列表项中显示它。 我在各个地方都尝试过@Input(),但是文本没有显示出来。可能有一些我遗漏或误解的东西,但这太吓人了,我不知道现在该怎么做或怎么做。我试图将“post-list-item”文件夹移动到“post-list”文件夹中,但它也不起作用。 因此,请注意“post-list”和“post-list-item”都是“app”的子组件。
谁能检查我的代码并告诉我如何使它工作,好吗?那将是真棒。 谢谢!!
app.component.html:
<div class="row">
<div class="col-12">
<app-post-list></app-post-list>
</div>
</div>
app.component.ts:
export class AppComponent {
title = 'blogApp';
pub1: string = "Ma première publi"; // this is what I want to display
}
post-list.component.html:
<ul class="list-group">
<app-post-list-item [titrePubli]="pub1"></app-post-list-item> <!-- this is where I tell that I want to display -->
<app-post-list-item></app-post-list-item>
<app-post-list-item></app-post-list-item>
</ul>
post-list.component.ts:
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-post-list',
templateUrl: './post-list.component.html',
styleUrls: ['./post-list.component.scss']
})
export class PostListComponent implements OnInit {
@Input() pub1: string; // note the @Input() here
constructor() { }
ngOnInit(): void { }
}
post-list-item.component.html:
<li class="list-group-item">
<h3 style="font-size: 20px;">Titre : {{ getTitrePubli() }}</h3> <!-- this is where I want to display -->
<h4 style="font-size: 18px;">Contenu : {{ getContenuPubli() }}</h4>
</li>
post-list-item.component.ts:
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-post-list-item',
templateUrl: './post-list-item.component.html',
styleUrls: ['./post-list-item.component.scss']
})
export class PostListItemComponent implements OnInit {
@Input() titrePubli: string;
contenuPubli: string = "C'est le bla bla bla";
@Input() pub1: string;
constructor() { }
ngOnInit(): void { }
getTitrePubli() { return this.titrePubli; }
getContenuPubli() { return this.contenuPubli; }
}
【问题讨论】:
标签: angular typescript input components display