Angular通过Input()在组件间传递数据时不会触发ngOnChanges.
创始人
2024-10-29 18:30:23
0

要解决这个问题,首先需要确保输入属性名字发生变化时会触发ngOnChanges。 因此,可以使用@Input装饰器的setter方法。这样,当输入属性更改时,setter将被调用,从而触发ngOnChanges。

示例如下:

// parent.component.ts import { Component } from '@angular/core';

@Component({ selector: 'app-parent', template:

Parent

, styleUrls: ['./parent.component.css'] }) export class ParentComponent { parentData = 'message from parent'; }

// child.component.ts import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';

@Component({ selector: 'app-child', template:

Child

{{ childData }}

, styleUrls: ['./child.component.css'] }) export class ChildComponent implements OnChanges { @Input() childData: string;

ngOnChanges(changes: SimpleChanges) { console.log('ngOnChanges - childData', changes); } }

在ChildComponent中的@Input("childData") getter方法将返回输入属性的值。 setter方法用于通知框架,当属性值被修改时需要更新值。

这样,当在父组件中修改值时,会触发ngOnChanges并更新子组件中的值。

相关内容

热门资讯

解密透视!wepoker分析,... 解密透视!wepoker分析,sohoo竞技联盟辅助器,练习教程(有挂技巧)-哔哩哔哩1、sohoo...
必备透视!sohoopoker... 必备透视!sohoopoker辅助!总是一直都是有辅助方法(有挂方法)-哔哩哔哩1、很好的工具软件,...
总结透视!哈糖大菠萝有没有挂,... 总结透视!哈糖大菠萝有没有挂,pokemmo手机脚本辅助器,诀窍教程(证实有挂)-哔哩哔哩pokem...
专业透视!pokemmo辅助官... 专业透视!pokemmo辅助官网!其实是真的有辅助软件(有挂秘笈)-哔哩哔哩1、专业透视!pokem...
了解透视!佛手在线是不是有挂,... 了解透视!佛手在线是不是有挂,德普之星app安卓版破解版,办法教程(有挂功能)-哔哩哔哩1、下载好德...
揭露透视!哈糖大菠萝破解器!一... 揭露透视!哈糖大菠萝破解器!一贯一直总是有辅助方法(有挂神器)-哔哩哔哩1、哈糖大菠萝破解器脚本辅助...
教你透视!wepoker私局代... 教你透视!wepoker私局代打,wepoker私人局俱乐部怎么进,要领教程(有挂头条)-哔哩哔哩运...
详情透视!agpoker辅助!... 详情透视!agpoker辅助!切实是有辅助教程(有挂教程)-哔哩哔哩1、上手简单,内置详细流程视频教...
了解透视!大菠萝789辅助器下... 了解透视!大菠萝789辅助器下载,wpk透视辅助下载,妙招教程(有挂详细)-哔哩哔哩所有人都在同一条...
分享透视!pokemmo辅助脚... 分享透视!pokemmo辅助脚本!其实真的有辅助方法(揭秘有挂)-哔哩哔哩1)pokemmo辅助脚本...