Angular中从模态组件调用主页组件函数未触发视图更新怎么办?
我需要从 ModalComponent 调用 HomeComponent 的 addItems 函数:
- 现状:调用后函数确实能正常执行,
console.log可以看到item_row数组已经新增了条目,但HTML页面没有新增对应商品行 - 预期效果:调用该函数时,主页表格可以自动追加新的商品行
HomeComponent 相关代码
import { Component, OnInit } from '@angular/core'; import { CommonComponent } from '../../common/common.component'; import $ from 'jquery'; import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) @Component({ selector: 'app-home', templateUrl: './home.component.html', styleUrls: ['./home.component.css'] }) export class HomeComponent implements OnInit { item_row: any[] = []; constructor(public common: CommonComponent) { } ngOnInit(): void { console.log(this.common.quote_company); this.item_row.push({ 'item_name': null, 'hsn_code': null, 'unit_price': null, 'qty': null, 'taxable_value': null, 'total': null }) } testJquery() { $('.test').html('test'); } addItems() { this.addRow(); } addRow() { const new_row = { 'item_name': 'test', 'hsn_code': 'test', 'unit_price': 'test', 'qty': 'test', 'taxable_value': 'test', 'total': 'test' } this.item_row.push(new_row); console.log(this.item_row); } }
我目前是在模态组件的HTML中调用主页组件的函数,期望调用后表格能自动追加新行,以下是 ModalComponent 的相关代码:
ModalComponent 相关代码
import { Component, OnInit, EventEmitter, Output } from '@angular/core'; import { AddQuoteComponent } from '../add-quote/add-quote.component'; import { CommonComponent } from '../../common/common.component'; import { HomeComponent } from '../home/home.component'; import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) @Component({ selector: 'app-modal', templateUrl: './modal.component.html', styleUrls: ['./modal.component.css'] }) export class ModalComponent implements OnInit { constructor( public home: HomeComponent, public common: CommonComponent ) { } ngOnInit(): void { } selectItem() { console.log('test'); this.home.addItems(); } }
核心问题原因
你通过依赖注入拿到的 HomeComponent 实例,和页面上实际渲染的 HomeComponent 并不是同一个对象。
虽然你给 HomeComponent 加了 @Injectable 装饰器,但Angular的组件实例是由模板渲染时创建的,注入拿到的是一个独立的服务单例,它的 item_row 数组修改不会同步到页面上渲染的组件实例,自然不会触发视图更新。
修正方案
Angular跨组件通信用以下三种常用方式,不要直接注入组件:
父子组件:通过
@Input/@Output传值
如果ModalComponent是HomeComponent的子组件,直接在ModalComponent里加@Output事件,点击时向上抛事件给HomeComponent处理:// ModalComponent 代码修改 @Output() addItemEvent = new EventEmitter<void>(); selectItem() { this.addItemEvent.emit(); }然后在
HomeComponent的模板里调用模态组件时绑定事件:<!-- HomeComponent 模板 --> <app-modal (addItemEvent)="addItems()"></app-modal>无关联组件:通过公共服务传值
创建一个专门的商品行公共服务,把item_row和addRow方法移到公共服务里,两个组件都注入这个服务调用:// item.service.ts (公共服务) @Injectable({ providedIn: 'root' }) export class ItemService { item_row: any[] = []; addRow() { const new_row = { 'item_name': 'test', 'hsn_code': 'test', 'unit_price': 'test', 'qty': 'test', 'taxable_value': 'test', 'total': 'test' }; this.item_row.push(new_row); } }HomeComponent和ModalComponent都注入这个ItemService,ModalComponent调用itemService.addRow()后,HomeComponent里绑定的item_row会自动触发视图更新。全局模态:用服务加状态广播
如果模态是全局弹出的,也可以用公共服务加BehaviorSubject做状态广播,HomeComponent订阅状态变化后更新自己的item_row。
内容的提问来源于stack exchange,提问作者Dinesh D

