当父值更改时,Angular 子组件不会更改值

时间:2021-04-04 03:36:26

标签: html angular typescript components

父组件通过routerLink从另一个组件(Home Component)获取数据。然后将这些数据发送到子组件。 这是第一次运行良好。 我的标题中有一个搜索栏,它在主页组件页面以及父组件和子组件呈现的页面中可见。单击搜索栏中的有效条目后,我再次将 idcategory 参数传递给父组件,但是,只有父组件发生更改,而子组件与之前的组件保持相同。 如何在不刷新的情况下再次将值传递给孩子?

父组件 HTML:

<body>
  <h1>{{id}}</h1>
  <h1>{{category}}</h1>
  <child-component [id] = "id" [category] = "category"> </child-component>
</body>

父组件 TS:

import { Component, OnInit ,ViewChild} from '@angular/core';
import { Router, ActivatedRoute, ParamMap } from '@angular/router';

@Component({
  selector: 'parent-component',
  templateUrl: './parent-component.component.html'
})
export class ParentComponent implements OnInit {

  id : any
  category : any

ngOnInit() {
      this.route.paramMap.subscribe(params => {
      this.id = params.get('id');
      this.category = params.get("media_type");})

//do other stuff

子组件 TS:

import { Component, OnInit , Input} from '@angular/core';

@Component({
  selector: 'child-component',
  templateUrl: './child-component.component.html'
})
export class ChildComponent implements OnInit {

  @Input() id: any;
  @Input() category: any;

//do other stuff

0 个答案:

没有答案