Angular 2对两个不同的路径

时间:2018-02-11 17:13:24

标签: angular angular2-routing

 { path: 'products/edit/:id', component: ProductEditComponent, resolve: {product: ProductResolver}},
 { path: 'products/view/:id', component: ProductDetailComponent, resolve: {product: ProductResolver}},




import { Injectable } from '@angular/core'
import { Resolve, ActivatedRouteSnapshot,Router,NavigationExtras } from '@angular/router'
import { ProductService } from './shared/product.service'

@Injectable()
export class ProductResolver implements Resolve<any> {
  constructor(private productService:ProductService,private router: Router) {}
  resolve(route: ActivatedRouteSnapshot) {            
   return this.productService.getProduct(route.params['id']);

 }}

// Both routes when going through the resolver go to the same address:

 this.router.navigate(['products/'+ id]);
 this.router.navigate(['products/edit/'+ id]);

我希望能够使用相同的解析器拥有两个具有两个不同路径的组件。每个组件都做不同的事情,但具有相同的对象(产品)。目前使用这样的代码,当通过解析器时,我总是转到product / id。虽然点击 this.router.navigate(['products / edit /'+ id])结尾是product / id  是否有可能以同样的决心去做?

1 个答案:

答案 0 :(得分:0)

尝试更改导航方式:

this.router.navigate(['products', id]);
this.router.navigate(['products', 'edit', id]);