Communication instance code between different components of Angularjs2 and angularjs2
The official method of AngualrJs2 is to use @ Input and @ Output to transfer values between components and ensure the parent-child relationship between components. The following is a simple method, implement data transfer between components, not only the parent and child components, but also the cross-module components.
/*** 1. define a service as the medium for passing parameters * // @ Injectable () export class PrepService {// define an attribute as a passing parameter between components, it can also be an object or method profileInfo: any;}/*** 2. the Component for passing parameters. In this simple demonstration, the parameter */@ Component ({selector: 'xxxxxxxx', templateUrl :". /XXXXXX.html ", styleUrls :[". /XXXXXXX.css "]}) export class ReportComponent {// defines the parameter to be passed (this is an object or a method) reponsePrep: any = {name:" bacon ", address: ""} // constructor (private ps: PrepService) injected into the PrepService {// assign the current component parameter to the profileInfo attribute ps of the PrepService. profileInfo = this. reponsePrep;}/*** 3. component that accepts parameters */@ Component ({selector: 'yyyy', templateUrl :". /YYYYYYYY.html ", styleUrls :[". /YYYYYYY.css "]}) export class commandComponent {// defines the parameter to receive the value requestPrep: any from the PrepService profileInfo attribute; // The constructor (private ps: prepService) {// assign the value of profileInfo attribute of PrepService to requestPrep to implement this. requestPrep = ps. profileInfo ;}}
Idea: define a service as a medium for passing parameters and inject it into the constructor of the component for passing parameters, and then attribute the Service (passing parameter Media) to assign values and values for passing parameters between components.
The above is the communication instance code between different Angularjs2 components introduced by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in time!