1

I have a string in one of the components and I would like to send it to another component when the submit button is pushed. I found solutions (input, emit...) but none of them worked.

This is my component from where I'd like to pass the variable:

export class UsersComponent {
  public data: string;

  public exportDocument() { // This function is called, when the submit button is pushed!
     this.data = doc.output("datauristring");
     // I have no idea what to write here, to pass this variable
  }
}

Other component:

export class CvPageComponent implements OnInit {
  public cvData; // This should be equal with the value of "data"
}
gmark11
  • 65
  • 1
  • 10
  • Does this answer your question? [Passing Data with Subjects and Proxies](https://stackoverflow.com/questions/49387889/passing-data-with-subjects-and-proxies) – Vikas Jun 02 '20 at 12:17
  • How is `UsersComponent` related to `CvPageComponent`? Are they siblings? Is one the parent of the other? Also why didn't input and emit work? Please share what you tried. – Mathyn Jun 02 '20 at 12:22

2 Answers2

0

In your cvPageComponent.html you use output

<user-component (cvData) = "getCvData($event)">

on your page you create a method that handle your cvDate like this:

public getCvData(cvData: any) { 
    // your code

  }

and in your userComponent you should mark cvData as output value like this

@output() cvData: EventEmitter<any> = new EventEmitter<any>

in your method:

public exportDocument() { 
// This function is called, when the submit button is pushed!
     this.data = doc.output("datauristring");
     cvData.emit(this.data);
  }
}
Uak
  • 66
  • 2
  • 8
0

If they have parent child relationship then you can use @Input way parent to child Demo

ViewChild or Sharing Data via Output() and EventEmitter for child to parent

If they are not related components. You can use

Sharing Data with a Service as a way Demo

storage method as a way you can use sessionStorage or localStorage etc...

sending parameter within url as a way Demo

mr. pc_coder
  • 16,412
  • 3
  • 32
  • 54