0

Below is my module structure:

- Albums-List
- Other Module 
  - Component 1 Landing
    - Component 2 in child route

Component two is displaying the albums list


Albums List Component

I have a component that obtains the route params to pass to a service to return a query.

albums$: Observable<Folder[]>;
@Output() parent_id = new EventEmitter<string>();
---
queryAndSubscribe(){
  this.albums$ = this._activatedRoute.params.pipe(
    tap (params => {
      this.parent_id.emit(params.parentID);
    }),
    switchMap(params => {
      return this.albumsService.getAlbums(params.parentID);
    })
  );
  this._subscription = this.albums$.subscribe(albums => {
    this.albumsArray = albums;
  });
}

Service

The function in the album list component calls this query in the service and returns the result.

  public getAlbums( parentID: string): Observable<Album[]> {
    return this.afs.collection<any>(`parent/${parentID}/albums`)
    .valueChanges({ idField: 'id'})
    .pipe(shareReplay());
  }

All of the above works fine, and my component can correctly display the list of albums and the parent_id output is updated fine.

The real issue now comes in when I have another component (component 2 edit) that also needs to display the album list component. This component is a child of another.

Component 2 parent module

const routes: Routes = [
  {
    path: '',
    component: LandingComponent,
    children: [
      {
        path: 'edit',
        loadChildren: () => import('editor.module').then(mod => mod.EditorModule),
      },    
    ]
  }
]

Component 2 - Edit.html

<app-album-list (parent_id)="updateParentID($event)"></app-album-list>

At this nested point, Component 2 isn't displaying any of the albums at all and I believe it's because the params.parentID is undefined, therefore not able to query the database correctly and return anything. As you can see above, i tried emitting the parentID into an output. But I think it's due to component 2's positioning that the activated route isn't the same.

Any ideas how to make the list-component return results in child components?

As a test, I modified the albums query to as follows:

this.albums$ = this._activatedRoute.parent.parent.params.pipe( and sure enough the albums display in component 2. However, there isn't a viable solution to allow the albums list component to be reusable anywhere in the app.

Haven
  • 87
  • 1
  • 9
  • you can try split responsibility between high level container and simple view component, it will less messy. some example how it work https://github.com/rnrn/Angular-MVC – Radik Apr 24 '20 at 13:13
  • Thanks for the response. Now sure how that would work? The structure i have in place at the moment works in all other areas and is necessary for the required UI design. The issue I'm having is that the albums cannot display in a child component in another route, as it doesn't have the ability to reference a relative activated route. – Haven Apr 24 '20 at 13:19
  • you can reproduce your issue in stackblitz maybe it give you a hint. as for me i make several projects with redux and it work perfectly – Radik Apr 24 '20 at 13:30

0 Answers0