A
A
Alexey2018-08-11 17:44:08
Angular
Alexey, 2018-08-11 17:44:08

Angular 6 how to properly pass service data using routing?

There is a shared service that processes and stores certain data:

@Injectable({
  providedIn: 'root'
})

export class DataService {
  public filteredMovies: any;

In one of the components, I fill the this._moviesService.filteredMovies property with some data.
After that, redirection to another component occurs using routing:
this.router.navigate(['/info/' + title + '/id/' + id]);

And already inside the info component, I'm trying to get the filteredMovies data from the service, but the problem is that the service has only preset data, the filteredMovies property is undefined:
import { Router } from '@angular/router';
import { ActivatedRoute } from '@angular/router';
...
  constructor(public _moviesService: MoviesService, private router: Router, private actRouter: ActivatedRoute) { }
...
 doSearch(id): void {
    // Здесь возникает ошибка (filteredMovies == undefined)
    this.asset = this._moviesService.filteredMovies[id];
 }

  ngOnInit() {
    this.actRouter.params.subscribe( params => this.doSearch(params['id']));
  }

Actually, the question is probably in understanding how shared services work in Angular, how can a component opened using routing inherit service information? By the way, I will say that the information that I am transmitting contains a complex array of objects.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
msdosx86, 2018-08-11
@azovl

It seems to work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question