angular 8中是否有一个生命周期挂钩,当单击elements按钮时会调用它

angular 8中是否有一个生命周期挂钩,当单击elements按钮时会调用它,angular,angular8,angular-lifecycle-hooks,Angular,Angular8,Angular Lifecycle Hooks,我有一个包含两个嵌套组件的主组件,其中一个组件包含输入字段(2)和一个用于添加项目的按钮,另一个同级组件在循环中显示项目 我有一个服务,它从输入组件中选取值,并在服务本身中更新其数组 每当我填写输入并提交服务时,就会将新项添加到数组中,但我需要更新需要显示此列表的同级组件 我知道我必须从服务获取值,因为它有一个更新的数组 我不知道在列表同级组件中可以使用哪个生命周期钩子来获取新列表,因为单击Add按钮时似乎没有调用它 我不想使用事件发射器 父组件 export class ShoppingCom

我有一个包含两个嵌套组件的主组件,其中一个组件包含输入字段(2)和一个用于添加项目的按钮,另一个同级组件在循环中显示项目

我有一个服务,它从输入组件中选取值,并在服务本身中更新其数组

每当我填写输入并提交服务时,就会将新项添加到数组中,但我需要更新需要显示此列表的同级组件

我知道我必须从服务获取值,因为它有一个更新的数组

我不知道在列表同级组件中可以使用哪个生命周期钩子来获取新列表,因为单击Add按钮时似乎没有调用它

我不想使用事件发射器

父组件

export class ShoppingComponent implements OnInit {
  ingredients:Ingredient[];

  constructor(private shoppingListService: ShoppingListService) { }

  ngOnInit() {
    this.ingredients = this.shoppingListService.getIngredients();
  }

} 
父html

<div class="row">
    <div class="col-md-10">
        <app-shoppinglistedit></app-shoppinglistedit>
        <hr>
        <app-shoppinglist></app-shoppinglist>
    </div>
</div>

它是html/模板

  form: FormGroup;

  constructor(
    fb: FormBuilder,
    private shoppingListService: ShoppingListService
  ) {
    this.form = fb.group({
      name: ['', Validator.required],
      amount: [0, [Validator.required, Validator.min(0)]]
    })
  }

  onAddNewItem(){
    const { name, amount } = this.form.value;
    const ingredient = new Ingredient(name, amount);
    this.shoppingListService.addIngredient(ingredient);
    console.log("All ingredients: ");
    console.log(this.shoppingListService.getIngredients());
  }
它是模板-html

<div class="row">
    <div class="col-md-10">
        <app-shoppinglistedit></app-shoppinglistedit>
        <hr>
        <app-shoppinglist></app-shoppinglist>
    </div>
</div>


名称
数量
添加项
删除项目
清除项

不使用EventEmitter会很棘手,会消耗CPU资源且无反应性。让我给你提个建议:

首先创建一个
@Input()配料:配料[]
到您的组件ShoppinglistComponent。您已经在父组件中获得了成分,因此您可以将它们传递给任何子组件(在本例中为ShoppinglistComponent) 然后将一个
@Output()ingredentadded=neweventemitter()
添加到您的组件shoppingListedComponent,您将在onAddNewItem方法中激发该组件。 在您的ShoppinglistComponent中,收听输出
(IngRediteded)=“InsertNewComponent($event)”

当您通过输入将配料传递到您的ShoppinglistComponent时,它将自动更新您的视图。不需要生命周期挂钩,只需使用一些公平的反应式代码

p-S:

您确实应该在您的ShoppingListedItemComponent中使用反应式表单或模板驱动表单,因为您希望管理表单并将其做好(不要重新构建轮子)。下面是一些使用反应式表单的代码(不要忘记包括表单模块反应式表单模块

  form: FormGroup;

  constructor(
    fb: FormBuilder,
    private shoppingListService: ShoppingListService
  ) {
    this.form = fb.group({
      name: ['', Validator.required],
      amount: [0, [Validator.required, Validator.min(0)]]
    })
  }

  onAddNewItem(){
    const { name, amount } = this.form.value;
    const ingredient = new Ingredient(name, amount);
    this.shoppingListService.addIngredient(ingredient);
    console.log("All ingredients: ");
    console.log(this.shoppingListService.getIngredients());
  }

名称
数量
添加项
删除项目
清除项

不使用EventEmitter会很棘手,会消耗CPU资源且无反应性。我建议您:

首先创建一个
@Input()配料:配料[]
到您的组件ShoppinglistComponent。您已经在父组件中获得了配料,因此您可以将它们传递给任何子组件(在我们的例子中是ShoppinglistComponent) 然后将一个
@Output()ingredentadded=neweventemitter()
添加到您的组件shoppingListedComponent,您将在onAddNewItem方法中激发该组件。 在您的ShoppinglistComponent中,收听输出
(IngRediteded)=“InsertNewComponent($event)”

当您通过输入将配料传递到您的ShoppinglistComponent时,它将自动更新您的视图。无需使用生命周期挂钩,只需使用一些公平的反应式代码即可

p-S:

您确实应该在ShoppingListedComponent中使用反应式表单或模板驱动的表单,因为您希望管理表单并将其做好(不要重新构建轮子)。下面是一些使用反应式表单的代码(不要忘了包括表单模块反应式表单模块

  form: FormGroup;

  constructor(
    fb: FormBuilder,
    private shoppingListService: ShoppingListService
  ) {
    this.form = fb.group({
      name: ['', Validator.required],
      amount: [0, [Validator.required, Validator.min(0)]]
    })
  }

  onAddNewItem(){
    const { name, amount } = this.form.value;
    const ingredient = new Ingredient(name, amount);
    this.shoppingListService.addIngredient(ingredient);
    console.log("All ingredients: ");
    console.log(this.shoppingListService.getIngredients());
  }

名称
数量
添加项
删除项目
清除项
insertNewIngredient(addedIngredient: Ingredient): void {
  this.ingredients = this.ingredients.concat(addedIngredient)
}
  form: FormGroup;

  constructor(
    fb: FormBuilder,
    private shoppingListService: ShoppingListService
  ) {
    this.form = fb.group({
      name: ['', Validator.required],
      amount: [0, [Validator.required, Validator.min(0)]]
    })
  }

  onAddNewItem(){
    const { name, amount } = this.form.value;
    const ingredient = new Ingredient(name, amount);
    this.shoppingListService.addIngredient(ingredient);
    console.log("All ingredients: ");
    console.log(this.shoppingListService.getIngredients());
  }