在resolver中加载视频的URL会有问题,因为它会在组件之前尝试加载数据。解决方法是直接在组件中加载视频的URL。以下是示例代码:
@Component({
selector: 'app-video',
template:
})
export class VideoComponent implements OnInit {
videoUrl: string;
constructor(private route: ActivatedRoute) {}
ngOnInit() { this.videoUrl = this.route.snapshot.data.videoUrl; } }
在路由中,您可以像这样指定resolver:
const routes: Routes = [ { path: 'video/:id', component: VideoComponent, resolve: { videoUrl: VideoResolver } } ];
在resolver中,您可以像这样加载视频的URL:
@Injectable()
export class VideoResolver implements Resolve
resolve(route: ActivatedRouteSnapshot) { let videoId = route.paramMap.get('id'); return this.videoService.getVideoUrl(videoId).pipe( catchError(error => { console.log('Error getting video URL'); return of(null); }) ); } }
通过这种方式,您可以确保只在需要时加载视频URL,并且可以在组件中处理错误情况。