如何在Resolve Angular 2中获取Route Params

Ray*_*per 8 angular2-routing angular

我试图让我的路线参与决心.但它不包含参数,但是当我从组件内部使用activatedRoute时它确实有效.我认为这是因为路线在决心内尚未改变.我如何让它工作?

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);
        })

    }
}
Run Code Online (Sandbox Code Playgroud)

app.route.ts

{
    path: 'post/:slug',
    component: PostComponent,
    data: {title: 'Post'},
    resolve: {
        post: PostResolver
    }
},
Run Code Online (Sandbox Code Playgroud)

Vic*_*hin 13

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

resolve(route: ActivatedRouteSnapshot) {
    console.log(route.data); // {title: "Post"}
}
Run Code Online (Sandbox Code Playgroud)

  • 上帝祝福你 (2认同)