Input are handy when a parent component communicates with its child. In your case, you want to rely on other patterns.
Services
A stateful service that holds the variable you want to use from one view to another can be the most reliable pattern you'd want to try.
Generate a service (using cli), and add a private behaviorSubject to it with its public accessors:
@Injectable({
providedIn: 'root',
})
export class MySingletonService {
private suggestion: BehaviorSubject<Suggestion> = new BehaviorSubject(null);
onSuggestionChange(): Observable<Suggestion> {
return this.suggestion.asObservable();
}
setSuggestion(nextSuggestion: Suggestion): void {
this.suggestion.next(nextSuggestion);
}
resetSuggestion(): void {
this.suggestion.next(null);
}
}
You can then set the suggestion in your first component, trigger the navigation, and access it in the second one.
export class MyFirstComponent {
constructor(
private myService: MyService,
private router: Router
) {}
setSuggestion(suggestion: Suggestion): void {
this.myService.setSuggestion(suggestion);
this.router.navigate(['to', 'second', 'component']);
}
}
export class MySecondComponent {
suggestion: Suggestion;
constructor(private myService: MyService) {}
ngOnInit(): void {
this.myService.onSuggestionChange().subscribe(suggestion => this.suggestion = suggestion);
}
}
Query Params
The second way to pass data is to use the url.
You can add parameters to your route by using the queryParams argument:
this.router.navigate(['url'], { queryParams: { suggestion }});
And then get in from the second component through ActivatedRoute:
this.route.queryParams.subscribe((params) => { this.suggestion = params.get('suggestion');}