Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly setup auxiliary routes in Angular 6?

I'm having an issue with routing in Angular 6. My app has a side menu on the right side, that updates as users navigate on the left-hand side of the page. This side menu doesn't show on the user-dashboard where the users projects are listed. It needs to show when the user selects a project and thus navigates to the project-home.

The organization can be seen in the image below. Once the user gets to project-home they should see the master/detail flow from elements-list to element-detail on the left-hand-side. Independently, under activities on the right-hand-side, a user should be able to click through the tabs for images, notes, and tasks.

Update: Here is a Stackblitz illustrating the issue: https://stackblitz.com/edit/zsoflin-routing

I'm at a loss, and would appreciate any direction on how to structure my routes. Thank you. -Zach

Map

app-routing.module.ts

`
const routes: Routes =[
  { path: 'user-dashboard', component: UserDashboard },
  { path: '', redirectTo: 'user-dashboard', pathMatch: 'full' },
  {
    path: 'project/:projectId',
    component: ProjectHomePage, 
    children:[
        {path: '', component: ElementsPage, pathMatch: 'full'},
        {path: 'element/:elementId', component: ElementPage, pathMatch: 'full'},
        {path: 'edit/:categoryId', component: EditFormPage, pathMatch: 'full'},
        {path: 'act', component:ActivitiesPage, 
            children: [
                {path:'images',outlet:'images',component: ImagesPage},
                {path:'notes',outlet:'notes',component: NotesPage},
                {path:'tasks',outlet:'tasks',component: TasksPage}
            ]
        },
    ]
  }
]
`

project-home.page.html

<ion-split-pane when="md">
<ion-menu type="push" menu-id="activityMenu" side="end" id="menucontent">
    <ion-router-outlet stack name="act"></ion-router-outlet>
</ion-menu>
<ion-router-outlet main></ion-router-outlet>

activities.page.html

<ion-tabs style="margin-top:56px;">
  <ion-tab label="Images" href="(images:images)">
    <ion-router-outlet name="images"></ion-router-outlet>
  </ion-tab>
  <ion-tab label="Notes" href="/tabs/(notes:notes)">
    <ion-router-outlet name="notes"></ion-router-outlet>
  </ion-tab>
  <ion-tab label="Tasks"" href="/tabs/(tasks:tasks)">
    <ion-router-outlet name="tasks"></ion-router-outlet>
  </ion-tab>
</ion-tabs>
like image 880
zsoflin Avatar asked Sep 28 '18 16:09

zsoflin


People also ask

What are auxiliary routes in Angular?

Angular supports the concept of auxiliary routes, which allow you to set up and navigate multiple independent routes in a single app. Auxiliary routes allow the user to access or toggle portions of the page, such as a side-bar or dialog, using the URL.

What is the correct way to add a basic route in Angular?

First, add links to the two components. Assign the anchor tag that you want to add the route to the routerLink attribute. Set the value of the attribute to the component to show when a user clicks on each link. Next, update your component template to include <router-outlet> .

What is auxiliary path?

Auxiliary Routes are independent, secondary routes that can be added to a page along with a primary route. As an example, we can have a side-menu that lives within the primary route with its own router-outlet : that's an auxiliary route.

Can we have two router outlets in Angular?

You can have multiple router-outlet in same template by configuring your router and providing name to your router-outlet, you can achieve this as follows. Advantage of below approach is thats you can avoid dirty looking URL with it.


1 Answers

I would do it like this

AppModule:

const routes: Routes =[
  { path: 'user-dashboard', component: UserDashboard },
  { path: '', redirectTo: 'user-dashboard', pathMatch: 'full' },
  {
    path: 'project',
    loadChildren: 'path.to.this.module#ProjectModule'
  }
];

ProjectModule:

const routes: Routes = [
  {
    path: '',
    component: 'ElementsPage' // If you really want the ProjectHomePage place here
  },
  {
    path: ':elementId',
    component: ElementPage
  },
  {
    path: ':categoryId/edit', // or 'edit/:categoryId'
    component: EditFormPage
  },
  {
    path: 'act',
    loadChildren: 'path.to.this.module#ActivityModule'
  }
];

ActivityModule:

const routes: Routes = [
  { path:'images', outlet:'images', component: ImagesPage },
  { path:'notes', outlet:'notes', component: NotesPage },
  { path:'tasks', outlet:'tasks', component: TasksPage }
];

Update: Your routes is working fine, you need now to route your outlet. Put your outlets nested your AppModule and in you app.component.ts you call:

ngOnInit() {
    this.router.navigate([{ outlets: { outletName: ['navigatingPath'] } }]);
  }

Them your outlets will show in like a popup. For more info how to do it, here is the main guide.

If you are trying to adding a sub menu in the right side, this is a strutural page called sidenav. I suggest you to add it in the main tree and show/hide when needed. See here, here and here

like image 111
Gaspar Avatar answered Oct 02 '22 19:10

Gaspar