Can't set up routing to a named outlet in Angular

自古美人都是妖i 提交于 2021-02-10 05:31:08

问题


I have three named router outlets as shown below.

...
<router-outlet name="menus"><router-outlet>
<router-outlet><router-outlet>
<router-outlet name="footer"><router-outlet>
...

In the markup I want to route the first one, menus, to a component with certain submenu junk in it as shown in the docs.

<ul *ngFor="let main of menus;" 
    routerLink="[{outlets:{menus:[{{main.link}}]}}]" 
    class="nav-items">{{main.header}}

The error I'm getting says that:

Error: Cannot match any routes. URL Segment: '%5B%7Boutlets:%7Bmenus:%5Bsubmenu1%5D%7D%7D%5D'

Am at a loss what's wrong with the syntax. Googling my fingernails off but haven't found a simple and crude example of a routerLink version showing how to point a route in a named outlet.

Edit: Based on the comments and samples, I need to reformulate the code being used, still with the same error. In the markup:

<ul *ngFor="let main of menus;" 
    (click)="pullMenu(main.link)" 
    class="nav-items">{{main.header}}

Then, in TS:

constructor(private router: Router, private route: ActivatedRoute) { }

pullSubmenu(input) {
  console.log(input);
  this.router.navigate(
    [{ outlets: { menus: [input] } }], 
    { relativeTo: this.route });
}

Now, I'm getting the following error (submenu1 is the name of configured path).

Error: Cannot match any routes. URL Segment: 'submenu1'

My routing is set up in the module like this.

const routes: Routes = [
  { path: "submenu1", component: Submenu1Component },
  { path: "submenu2", component: Submenu2Component }
];

@NgModule({
  declarations: [
    AppComponent,
    NavbarComponent,
    MainAreaComponent,
    Submenu1Component,
    Submenu2Component
  ],
  imports: [
    BrowserModule,
    RouterModule.forRoot(routes)
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

回答1:


You need to use the evaluated version [routerLink]:

<ul *ngFor="let main of menus;" 
    [routerLink]="[{outlets:{menus:[{{main.link}}]}}]" 
    class="nav-items">{{main.header}}

As an alternative you can emulate the routerLink. Here is the gist of what it does:

  @HostListener('click')
  onClick(): boolean {
    const extras = {
      skipLocationChange: attrBoolValue(this.skipLocationChange),
      replaceUrl: attrBoolValue(this.replaceUrl),
    };
    this.router.navigateByUrl(this.urlTree, extras);
    return true;
  }

So, here is the setup using navigate instead of navigateByUrl:

   @Component({
      template: `
        <ul *ngFor="let main of menus;" (click)="[{outlets:{menus:[{{main.link}}]}}])"
        class="nav-items">{{main.header}}
      `
   ...
   class MyComponent {
      constructor(router: Router, route: ActivatedRoute) {}

      navigate(commands) {
         this.router.navigate(commands, {relativeTo: this.route})

You can't use unevaluated version of routerLink because it reads commands as a string and if you have outletsin the commands strings don't work. See Navigation to secondary route URL for routerLink attribute to understand why.



来源:https://stackoverflow.com/questions/45758795/cant-set-up-routing-to-a-named-outlet-in-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!