AngualrJs2 The official method is to @input, @Output to achieve the mutual value between components, and the components must be a parent-child relationship, the following gives you a simple way to achieve the transfer of values between components, not only the parent-child component, cross-module components can also achieve the value
/**. Define a service as the medium for passing parameters */@Injectable () Export class prepservice{ //define a property as a pass-through parameter between components, or it can be an object or method Profileinfo:any; }
/**. Pass the parameters of the component, I have a simple demonstration here, directly in the constructor implementation of the parameter */@Component ({ selector: ' XXXXXXX ', templateurl: "./xxxxxx.html", styleurls:["./xxxxxxx.css"]}) Export class Reportcomponent { //define the parameters to pass (here is an object or method) reponseprep:any ={ name: Bacon Bean curd, Address: "Central Europe Pork" } //constructor Injection Prepservice service constructor (private Ps:prepservice) { // Assigns the current component parameter to Prepservice's ProfileInfo property ps.profileinfo = This.reponseprep; }}
/**. The component that accepts the parameter */@Component ({ selector: ' yyyyyy ', templateurl: "./yyyyyyyy.html", styleurls:["./ Yyyyyyy.css "]}) Export class Commandcomponent { //define parameter to receive value Requestprep:any from Prepservice service ProfileInfo property ; Constructor injection Prepservice Service constructor (private Ps:prepservice) { // Assigns the value of the ProfileInfo property of the Prepservice to the value of the Requestprep implementation component This.requestprep = Ps.profileinfo; }}
Idea: Defining a service as a medium for passing parameters is injected into the constructor of the component to which the parameter is being passed, and then assigning values and values to the service's Inside property (the reference vector) to implement the parameters between the components
Above demo just give a simple idea to everyone, everyone can play freely
Original-ANGULARJS2 communication between different components