简体   繁体   English

辅助路由如何在Angular2 RC5 / Router3 RC1中工作?

[英]How do aux routes work in Angular2 RC5/Router3 RC1?

There is hardly any information regarding auxiliary routes on the web, even less so for RC5/RC1, I hope someone here on SO managed to get them to work (they are supposed to have been fixed or so I heard, but that's about it). 在网上几乎没有任何关于辅助路线的信息,对RC5 / RC1来说更是如此,我希望有人在这里设法让他们上班(他们应该被修复或者我听说过,但那就是关于它) 。

I got the following routing declaration: 我收到了以下路由声明:

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

import { ContactsComponent } from './contacts.component';
import { NewContactComponent } from './new-contact.component';

const contactsRoutes: Routes = [
    { path: '', component: ContactsComponent },
    { path: 'new', component: NewContactsComponent, outlet: 'form' }
];

export const contactsRouting = RouterModule.forChild(contactsRoutes);

Here's the routerLink and outlet on ContactsComponent : 这是ContactsComponent上的routerLinkoutlet

<a [routerLink]="['.', 'form:new']"> 
    <button md-fab class="md-fab">
        <md-icon class="md-24">add</md-icon>
    </button>
</a>
<router-outlet name="form"></router-outlet>

However, this only leads to error messages such as 但是,这只会导致错误消息,例如

Error: Cannot match any routes: 'contacts/form%3Anew' 错误:无法匹配任何路线:'contacts / form%3Anew'

Does anyone have a working example for Angular2 RC5/Router3 RC1? 有没有人有Angular2 RC5 / Router3 RC1的工作示例?

I finally got this to work (use the source, Luke), with a twist for now, but I'll keep on investigating until I find the complete solution. 我终于得到了这个(使用源,Luke),现在有一个转折,但我会继续调查,直到找到完整的解决方案。

Given this routing setup 鉴于此路由设置

const appRoutes: Routes = [
    { path: '', redirectTo: 'welcome', pathMatch: 'full' },
    { path: 'welcome', component: WelcomeComponent },
    { path: 'employee/:id', component: EmployeeDetailComponent }
    { path: 'chat', component: ChatComponent, outlet: 'aux' }
];

that is, with the aux route in the root path (this is the twist I mentioned), I can write 也就是说,根路径中的aux路径(这是我提到的扭曲),我可以写

<a [routerLink]="['/', { outlets: { primary: 'employee/14', aux: 'chat' } }]"

and it translates to this route: 它转换为这条路线:

/employee/14(aux:chat)

Clicking on this link then actually renders the component in the outlet, 单击此链接然后实际呈现插座中的组件,

import { Component } from '@angular/core';

@Component({
    selector: 'my-app',
    template: '<router-outlet></router-outlet><router-outlet name="aux"></router-outlet>'
})
export class AppComponent {}

The full plnkr can be found here . 完整的plnkr可以在这里找到。

Once I get this to work for child components I'll update this answer. 一旦我将其用于子组件,我将更新此答案。

As far as I know it should be something like 据我所知它应该是这样的

<a [routerLink]="[{ outlets: { 'aux': ['/employee/14/chat'] } }]">

See also https://angular.io/docs/ts/latest/api/router/index/RouterLink-directive.html 另见https://angular.io/docs/ts/latest/api/router/index/RouterLink-directive.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM