简体   繁体   English

从其他组件/架构问题中获取价值?

[英]Get value from other component / architecture issue?

I am completely new to Angular, have started it last week (but I have a small basic background in programming).我对 Angular 完全陌生,上周开始使用它(但我有一点编程的基本背景)。 I am trying to build a very basic blog application, and I want to get a value from the root (app.component.ts) to my component tag(s) "app-post-list-item", located in my other component template (post-list-component.html), so that I can display it in my post list item.我正在尝试构建一个非常基本的博客应用程序,并且我想从根(app.component.ts)获取一个值到我的组件标签“app-post-list-item”,位于我的其他组件中模板(post-list-component.html),以便我可以在我的帖子列表项中显示它。 I have tried @Input() in various places, but the text just doesn't show up.我在各个地方都尝试过@Input(),但是文本没有显示出来。 There might be something I am missing or misunderstanding, but this is just all so intimidating and I don't know what or how to do now.可能有一些我遗漏或误解的东西,但这太吓人了,我不知道现在该怎么做或怎么做。 I have tried to move the "post-list-item" folder inside the "post-list" one, but it doesn't work either.我试图将“post-list-item”文件夹移动到“post-list”文件夹中,但它也不起作用。 So, note that both "post-list" and "post-list-item" are children components of "app".因此,请注意“post-list”和“post-list-item”都是“app”的子组件。

Can anyone check my code and tell me how I could make that work, please?谁能检查我的代码并告诉我如何使它工作? That would be awesome.那将是真棒。 Thank you!!谢谢!!

This is my architecture.这是我的架构。

app.component.html: app.component.html:

<div class="row">
  <div class="col-12">
    <app-post-list></app-post-list>
  </div>
</div>

app.component.ts: 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: 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: 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:后列表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: 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; }
}

The attached screenshot is not the architecture, it is the file structure of the project.随附的屏幕截图不是架构,它是项目的文件结构。 And it has nothing to do with the data sharing as long the files are correctly referenced in the module and other files where they are used.只要文件在模块和使用它们的其他文件中正确引用,它与数据共享无关。

The source of the confusion here is the naming scheme.这里混乱的根源是命名方案。 Let's look at one simple line让我们看一个简单的行

<app-post-list [pub1]="pub1"></app-post-list>
                 ˄       ˄
                 |       |
        @Input()         member
        variable         variable
        of post-list     of app-component

As you can see, [pub1] refers to the input variable of post-list component and pub1 on the right side refers to the member variable value ("Ma première publi" in your case).如您所见, [pub1]指的是post-list组件的输入变量,右侧的pub1指的是成员变量值(在您的情况下为“Ma première publi”)。

While the above pattern isn't incorrect, if you are beginning Angular, it might serve you well to have some obvious naming patterns.虽然上面的模式不是错误的,但如果你是从 Angular 开始的,那么有一些明显的命名模式可能对你很有帮助。 For eg.,例如,

<app-post-list [pub1Input]="pub1Value"></app-post-list>

Now there is a clear distinction between the variable names and it helps understanding better.现在变量名称之间有明显的区别,它有助于更好地理解。 Then you send the variable again from post-list to post-list-item component.然后再次将变量从post-list发送到post-list-item组件。 I've modified your code to following and it works as expected.我已将您的代码修改为以下代码,它按预期工作。

App component应用组件

export class AppComponent  {
  title = 'blogApp';  
  pub1Value: string = "Ma première publi";
}
<div class="row">
  <div class="col-12">
    <app-post-list [pub1Input]="pub1Value"></app-post-list>
  </div>
</div>

Post list component帖子列表组件

export class PostListComponent implements OnInit {
  @Input() pub1Input: string;

  titrePubliOne = 'title 1';
  titrePubliTwo = 'title 2';
  titrePubliThree = 'title 3';

  constructor() { }

  ngOnInit() { }
}
<ul class="list-group">   
  <app-post-list-item [titrePubli]="titrePubliOne" [pub1]="pub1Input"></app-post-list-item>
  <app-post-list-item [titrePubli]="titrePubliTwo" [pub1]="pub1Input"></app-post-list-item>
  <app-post-list-item [titrePubli]="titrePubliThree" [pub1]="pub1Input"></app-post-list-item>
</ul>

Post list item component发布列表项组件

export class PostListItemComponent implements OnInit {
  @Input() titrePubli: string;
  @Input() pub1: string;
  contenuPubli: string = "C'est le bla bla bla";

  constructor() { }

  ngOnInit() { }
}
<li class="list-group-item">
  <h3 style="font-size: 20px;">Titre : {{ titrePubli }}</h3>
  <h4 style="font-size: 18px;">Contenu : {{ pub1 }}</h4>
</li>

Working example: Stackblitz工作示例: Stackblitz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 angular 10 中从一个组件到其他组件获取 onclick 值 - how to get onclick value from a component to other component in angular 10 如何在 angular 6 中单击从一个组件到另一个组件的按钮时检查复选框的值 - How to get checked value of checkbox on click a button from one component to other component in angular 6 如何从一个组件获取值或数组到另一个组件而不存储在 angular 8 的本地存储中 - How to get the value or array from one component to other component without storing in localstorage in angular 8 angular中如何从一个组件获取选择的选项值到另一个组件 8 - How to get the selected option value from one component to other component in angular 8 在运行时初始化值时如何从其他组件获取变量的值? - How to get a value of a variable from other Component when value is initialized on runtime? 角度:无法从其他组件获得价值 - Angular: cannot receive value from other component 如何使用其他组件的插值? - How to use interpolation value from other component? 来自其他组件的Typescript / Angular 2 get方法 - Typescript / Angular 2 get method from an other component angular 无法从其他组件获取数据 - angular cannot get data from other component 如何在angular 8中从一个组件到另一个组件获取文本框的值,反之亦然 - How to get the value of text box from one component to other and vice versa in angular 8
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM