模块间通信的话angular2+可以使用@Input() 和 @Output()进行父子组件间通信
例如
父组件html
<app-input-output [item]="currentItem" (deleteRequest)="crossOffItem($event)">
</app-input-output>
子组件ts
export class ItemDetailComponent {@Input() item = ''; // decorate the property with @Input()
}
但是这种方式在多层父子组件情况下会变的非常困难因为可能需要因为一次通信而管理每个模块中的该变量,在集成系统中更无法实现因为不同项目之间无法进行通信
这里的话提供一个解决方案的思路,即编写一个公共项目,这个公共项目作为“依赖(node_module)”引入各集成项目中,作为通信中间件,公用工具库与公用样式库,组件库
在这种情况下我们仅需要在公共项目中定义一个服务,例如
@Injectable({ providedIn: 'root' })
export class PublicService {status: 'open'
}
在集成系统中引入该依赖中的该服务进行公用变量的获取与操作即可
import { PublicService } from 'publicModule';```
constructor( public publicService: PublicService) {// 若获取不到可能需要进行异步处理 settimeout,因为组件同时加载settimeout(() => {console.log('publicService', this.publicService.status);// todothis.publicService.status = 'close';}, 500)
}