使用Angular 2,是否可以使子路径不显示在主标记
中<router-outlet></router-outlet>
例如:
url : "http://mywebsite.com/"
MainComponent.ts
@Component({
...
template:'<router-outlet></router-outlet>'
...
})
@RouteCongif([
{path: '/products', name:'Product', component: Product}
])
这将子组件显示在&lt; router-outlet&gt;中。标签
好的,现在可以进行这种配置:
url : "http://mywebsite.com/products"
ProductComponent.ts
@Component({
template: `
...
<div> My list of products ! </div>
...
<a [RouteLink]="['ProductDetails', {slug-product-details:product.slug}]">
{{ product.name }} Details
</a>
...
<sub-router-outlet></sub-router-outlet>
`
})
@RouteConfig([
{path: '/:slug-product-details', name:'ProductDetails', component: ProductDetailsComponent},
])
和
url : "http://mywebsite.com/products/one-product-details"
ProductDetailsComponent.ts
@Component({
...
template : `
<div> Details of the product : </div>
...
`
...
})
我想保留使用自动设计网址的路由器,并将路由和详细信息模板显示到这种&lt; sub-router-outlet&gt;标签
感谢您的帮助。
答案 0 :(得分:6)
警告:以下代码仅适用于Angular2 Beta
您可以实施子路由。您的文件结构应该类似于此。
<强> app.ts 强>
@RouteConfig([
...
{ path: '/product/...', component: Product, name: 'Product'}
{ path: '/home', component: Home, name: 'Home'}
...
])
export class App { }
<强> product.ts 强>
@Component({
selector: 'product',
templateUrl: 'app/components/product/product.html',
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
...
{ path: 'product-one', component: ProductOne, name: 'Product-one' },
{ path: 'product-two', component: ProductTwo, name: 'Product-two', useAsDefault: true },
...
])
export class Product {
constructor(location: Location, public router: Router) {}
goToProductOne() {
this.router.navigate(['Product','Product-one'])
}
}
<强> product.html 强>
...
<a [routerLink]="['./Product-one']"> Product One </a>
<a [routerLink]="['/Home']"> Home </a>
...
其中['./Product-one']是子路由,['/ Home']是父路由