在我的用于通过

问题描述 投票:0回答:1
routing传递动态状态的代码下面

<a [routerLink]="['/article', row.slug ]" [state]="{ hello: 'world' }" class="preview-link"> <span>{{row.slug }}</span> </a>

此状态显示在我的解析器中,但是当尝试通过我的组件访问它时,它是未定义的

我的解析器

import { Injectable, } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot, ActivatedRoute, 
NavigationStart } from '@angular/router';
import { Observable } from 'rxjs';

import { catchError } from 'rxjs/operators';
import { Article } from '../../entities';
import { UserService } from './../../../../shared/shared-services/core';
import { ArticlesService } from '../../services';


import { filter, map } from 'rxjs/operators';

@Injectable()
export class ArticleResolver implements Resolve<Article> {


constructor(
private articlesService: ArticlesService,
private router: Router,
private userService: UserService
) {}

resolve(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<any> { 

// state printed successfully here 
console.log(this.router.getCurrentNavigation().extras.state);

return this.articlesService.get(route.params['slug'])
  .pipe(catchError((err) => this.router.navigateByUrl('/')));
}
}

我的组件

constructor( private route: ActivatedRoute, private articlesService: ArticlesService, private commentsService: CommentsService, private router: Router, private userService: UserService, public deviceService: DeviceDetectorService ) { } ngOnInit() { // Retreive the prefetched article this.route.data.subscribe( (data: { article: Article }) => { this.article = data.article; // Here i trying to display state but it's print undefined console.log(this.router.getCurrentNavigation().extras.state); } );
在获取所需数据之后,将动态状态[state] =“ {hello:'world'}”传递到解析器,然后传递到组件,在解析器中显示的状态,但是无法在我的组件中显示状态,因为它是...
angular typescript angular-material
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.