Raymond the Developer
Raymond the Developer

Reputation: 1676

How to get Route Params inside Resolve Angular 2

I am trying to get my route params inside a resolve. However it doesn't contain the params but when I use activatedRoute from inside a component it does work. I think it's because the route hasn't changed yet inside the resolve. How do I get this to work?

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

Upvotes: 12

Views: 16650

Answers (3)

Deeksha Bilochi
Deeksha Bilochi

Reputation: 94

resolve(route: ActivatedRouteSnapshot): Observable<any> {
return this.property.getProperty(parseInt(route.paramMap.get(' ')));
}

Upvotes: -2

Manichandra
Manichandra

Reputation: 185

resolve(route: ActivatedRouteSnapshot) {
   console.log(route.data.title); // "Post"
   console.log(route.params['slug']) // prints the dynamic parameter value
}

Upvotes: 3

Victor Bredihin
Victor Bredihin

Reputation: 2360

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

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

Upvotes: 22

Related Questions