2017-01-27 59 views
1

我想讓我的路線參數裏面的決心。 但它不包含參數,但是當我使用組件內部的activatedRoute時,它確實有效。我認爲這是因爲路線在決議中還沒有改變。我如何得到這個工作?如何獲得路線參數裏面解決角2

post.resolve.ts

@Injectable() 
export class PostResolver implements Resolve<any> { 
    slug: any; 

    constructor(
     private activatedRoute: ActivatedRoute, 
     private memoryService: MemoryService, 
     ) {} 

    resolve() { 

     console.log(this.activatedRoute); 

     this.activatedRoute.params.subscribe(params => { 
      this.slug = params['slug']; 
      console.log(this.slug); 
      return this.memoryService.getPost(this.slug); 
     }) 

    } 
} 

app.route.ts

{ 
    path: 'post/:slug', 
    component: PostComponent, 
    data: {title: 'Post'}, 
    resolve: { 
     post: PostResolver 
    } 
}, 

回答

3
import { Resolve, ActivatedRouteSnapshot } from '@angular/router'; 

resolve(route: ActivatedRouteSnapshot) { 
    console.log(route.data); // {title: "Post"} 
}