Angular 2:子路由的多个

2024-01-08

使用 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}
])

这会将子组件显示到 标记中

好吧,现在可以有这样的配置吗:

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},
])

And

url : "http://mywebsite.com/products/one-product-details"
ProductDetailsComponent.ts
@Component({
    ...
    template : `
         <div> Details of the product : </div>
         ...
    `
    ...
})

我想保留使用自动设计的 url 的路由器,并将路由和详细信息模板显示到这种 标签中。

感谢您的帮助。


WARNING:下面的代码仅适用于 Angular2 Beta

您可以实现子路由。您的文件结构应该类似于这样。

app.ts

@RouteConfig([
    ...
    { path: '/product/...', component: Product, name: 'Product'}
    { path: '/home', component: Home, name: 'Home'}
    ...
])
export class App { }

产品.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'])
    }
}

产品.html

...
<a [routerLink]="['./Product-one']"> Product One </a>
<a [routerLink]="['/Home']"> Home </a>
...

其中 ['./Product-one'] 是子路由,['/Home'] 是父路由

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular 2:子路由的多个 的相关文章

随机推荐