4

I have a lazy module which I want a different experience for desktop and mobile device. Basically I want my desktop layout like: Desktop layout

Component1 display a list, user chooses an item on the list and component2 will display the details. I created router-outlet named 'side' to display as a sidebar. Here's is the router config:

const desktop_routes: Routes = [
  {
    path: '',
    component: LayoutComponent,
    children: [
      {
        path: '',
        component: ListComponent,
        outlet: 'side'
      },
      {
        path: '',
        redirectTo: 'detail',
        pathMatch: 'full'
      },
      {
        path: 'detail',
        component: DetailComponent,
      }
    ]
  }

In mobile layout, I want Component1 as the content of path: '', user choose any time from the list will be lead to detail page (2 separate page): enter image description here

How can I do it?

2 Answers 2

2

Use the same child components but create 2 route lists: /desktop/component2 and /mobile/component2. Then in desktop parent show component3a containing the desktop layout. In mobile layout set the routes up so that component3a has a single outlet and loads the pages all into a single router-outlet, if that makes sense?

Sign up to request clarification or add additional context in comments.

Comments

0

I suggest another approach. If you know when you're in a "mobile" or in a "desktop" (really you should check the "screen.size"). see, e.g. this SO

You can has some like

<ng-container *ngIf="{size:commonService.resize$|async} as size">
  <div #detail *ngIf="size.size?.width>500">
  <router-outlet></router-outlet>
  </div>
</ng-container>

In your component you try to "get" the div detail

@ViewChild('detail') detail:ElementRef<any>

know you can check if "detail" exist and navigate to one or another one direction

click(){
    if (this.detail)
       this.router.navigate('parent/child/1')
    else
       this.router.navigate('parent-child/1')

}

You router definition like

const routes: Routes = [
  { path: 'parent', component: ParentComponent,children:[
       {path:'child:id', component:ChildrenComponent
   ] },
  { path: 'parent-child:id', component: ChildrenComponent },
];

yet you can reach the child-component using parent-child/1 in the "main" router-outlet or using parent/child/1 (in the "main" router-outlet you has "parent" and in the router-outlet of "parent" you has the child)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.