角度2:多个< router-outlet>对于子路线 [英] Angular 2: multiple <router-outlet> for sub routes
问题描述
使用Angular 2,是否可以使子路由不显示在主标签中
With Angular 2, is it any way to have sub route not displaying into the main tag
<router-outlet></router-outlet>
例如:
url : "http://mywebsite.com/"
MainComponent.ts
@Component({
...
template:'<router-outlet></router-outlet>'
...
})
@RouteCongif([
{path: '/products', name:'Product', component: Product}
])
这会将子组件显示在< router-outlet>中.标签
This displays the sub component into the <router-outlet> tag
好的,现在可以进行这种配置了:
All right, now is it possible to have this kind of configuration :
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>
...
`
...
})
我想通过自动设计的url保留路由器的使用,并将路由和详细信息模板显示在这种< sub-router-outlet>中.标签.
I want to keep the usage of the router with the auto designed url and diplay the route and the details template into this sort of <sub-router-outlet> tag.
谢谢您的帮助.
推荐答案
警告:以下代码仅适用于Angular2 Beta
WARNING: The code below only works on Angular2 Beta
您可以实现子路由.您的文件结构应该类似这样.
You can implement sub routing. Your file structure should be similar like this.
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']是父路由
Where ['./Product-one'] is a subroute and ['/Home'] is a parent route
这篇关于角度2:多个< router-outlet>对于子路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!