gpt4 book ai didi

javascript - Angular 2 : multiple for sub routes

转载 作者:数据小太阳 更新时间:2023-10-29 05:18:23 25 4
gpt4 key购买 nike

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

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

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

感谢您的帮助。

最佳答案

警告:下面的代码只适用于 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']是父路由

关于javascript - Angular 2 : multiple <router-outlet> for sub routes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34439401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com