AngualrJs2 official method is to use @Input and @Output to realize the mutual transmission of values between components, and the relationship between components must be a parent-child relationship. Here is a simple method to realize the transmission of values between components. Not only parent-child components, but also cross-module components can also implement the transmission of values.
/** *1. Define a service as a medium for passing parameters */ @Injectable() export class PrepService{ //Define a property as a parameter passed between components, or it can be an object or method profileInfo: any; } /** *2. The component that passes parameters, I will briefly demonstrate here, and directly implement the parameter transfer in the constructor. */ @Component({ selector: 'XXXXXXX', templateUrl:"./", styleUrls:["./"] }) export class ReportComponent { //Define the parameters to be passed (here is an object, or a method) reponsePrep:any ={ name : "Bao pork skin", address:"Central European Pork Belly" } //Constructor injects PrepService service constructor(private ps:PrepService){ // Assign the current component parameter to the profileInfo property of PrepService = ; } } /** *3. Components that accept parameters */ @Component({ selector: 'YYYYYY', templateUrl:"./", styleUrls:["./"] }) export class commandComponent { //Define the parameter to receive the value from the PrepService service profileInfo attribute requestPrep:any; //Constructor injects PrepService service constructor(private ps:PrepService){ // Assign the value of the profileInfo attribute of PrepService to the transmission value between the requestPrep implementation components = ; } }
Idea: Define a service as the medium for passing parameters and inject it into the constructor of the component to be passed, and then assign values and values to the attributes (parameter media) in the service to implement parameters between components.
The above is the communication example code between different components of Angularjs2 introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message. The editor will reply to you in time!