3

I recently started a new project based on angular 2.0.0 in combination with Angular Router (V3.0.0). Thereby I have issues with the auxiliary routes.

I want to split up my application in different views that appear in every state like navigation bar, main content, footer etc. So that when the user interact with the site only the part of the page that needs to be changed (f.e. the content) is changed and all other views are left as they are. Therefore I wanted to use the routers auxiliary feature as discussed in this talk:

Angular Router Talk @AC2015

I set up my files as follows:

app.html

<router-outlet name="navigation"></router-outlet>

<main>
    <router-outlet></router-outlet>
</main>

<router-outlet name="footer"></router-outlet>

app.routes.ts

import {Routes} from '@angular/router';

export const rootRouterConfig: Routes = [
  {path: '', redirectTo: 'start(navigation:/navigation)', pathMatch: 'full'}
];

start.routes.ts

import {Routes} from '@angular/router';
import {StartContentComponent} from './startContent/startContent.component';
import {StartNavigationComponent} from "./startNavigation/startNavigation.component";  

export const startRouterConfig: Routes = [
    {path: 'start', component:  StartContentComponent},
    {path: 'navigation', component: StartNavigationComponent, outlet: 'navigation'}
];

Now the problem is: If I manually enter

http://localhost:3000/#/start(navigation:/navigation)

into the url of the browser both components are displayed correctly. But the redirect in app.routes.ts is not working and I get the following error when accessing http://localhost:3000/#/:

Uncaught (in promise): Error: Cannot match any routes: ''

The official Angular 2 docs say nothing about auxiliary routing yet only that it will be discussed in the future. Apart from that I found some other blogposts but none of them discussed auxiliary routes in combination with redirects.

Does anyone know what my problem is or have similar issues?

Is there another approach to structure my page to achieve what I want?

Should I maybe switch to Ui-router?

1

3 Answers 3

3

I had the same problem, and Mr. Binary's solution seemed to be on the right track, but the 'Componentless Routes' part of this site got me there.

My solution:

export const RedirectionIncludingAuxRoutes: RouterConfig = [
    {
        path: 'redirect-me',
        redirectTo: 'redirected-with-aux'
    },
    {
        path: 'redirected-with-aux',
        children: [
            {
                path: '',
                component: PrimaryComponent
            },
            {
                path: '',
                component: SecondaryComponent,
                outlet: 'auxiliary'
            }
        ]
    }
]
2
  • 1
    Thanks a lot! Sorry for my answer taking so long. Now it's working fine! Commented Oct 5, 2016 at 17:35
  • here it works without any URL change and the example you showed has unnecessary redirectTo: 'details' as it does not have 'details' route I tried putting default instead, but it didn't work. My motive is that navigation to 'team/11’ should result in 'team/11/(list/default//aux:details). Commented Jan 5 at 12:04
0

That's recommended approach when you have more changing parts.
Anyway try changing

redirectTo: 'start(navigation:/navigation)'
to
redirectTo: 'start/(navigation:navigation)'

And

{path: 'start', component:  StartContentComponent},
{path: 'navigation', component: StartNavigationComponent, outlet: 'navigation'}
to
{path: 'start', component:  StartContentComponent,
   children: [
      {path: 'navigation', component: StartNavigationComponent, outlet: 'navigation'}
]},

Also i would recommend this article to router:
http://blog.angular-university.io/angular2-router/

2
  • I changed it as you supposed but that didn't change the error. It's still not working :/ Thanks, but I already worked through this article. As already mentioned, there are not many resources about the new router. Commented Sep 18, 2016 at 7:21
  • 1
    Sorry, the proposed solution doesn't seem to work. If I open the page I get the error: " Error: Cannot match any routes: '' Commented Oct 5, 2016 at 17:29
0

You are almost there. Just one step away. You need to wire rootRouterConfig in one of the following ways

import {Routes , RouterModule } from '@angular/router';

@NgModule({
  imports: [RouterModule.forRoot(rootRouterConfig)],
  exports: [RouterModule],
})

OR

import {ModuleWithProviders } from '@angular/core';
import {Routes , RouterModule } from '@angular/router';

export const routing: ModuleWithProviders =RouterModule.forRoot(rootRouterConfig);

Let me know which one works for you.

Not the answer you're looking for? Browse other questions tagged or ask your own question.