简体   繁体   English

路由解析器角度检查数据是否为空

[英]Route resolver angular check if data is empty

I have a resolver like this: 我有一个这样的解析器:

export class TaskResolver implements Resolve<Documents> {

  constructor(private taskService: TaskService) { }

  resolve(route: ActivatedRouteSnapshot): Observable<Documents> {
    const taskId = route.params.taskId;
    return this.taskService.DocumentsTask(taskId);
  }
}

And inside my component I have: 在我的组件中,我有:

this.route.data.subscribe((data: { documents: Documents }) => {
     if (data.documents) {
    this.documents = data.documents;
  } else {
    this.router.navigate([`/error/404`]);
  }
});

I need to move the this.router.navigate([ /error/404 ]); 我需要移动this.router.navigate([ /error/404 ]); inside resolver, that way the resolver will check if the data is empty and redirect to error, and not the component. 在解析器内部,通过这种方式,解析器将检查数据是否为空并重定向到错误,而不是组件。 Or maybe move to auth guard that looks like this 或者也许转到看起来像这样的身份验证防护

canActivate(route: ActivatedRouteSnapshot): boolean {
    const taskId = route.params.taskId;
    if (taskId) {
      return true;
    }
  }

DocumentsTask(taskId) returns Observable DocumentsTask(taskId)返回Observable

The CanActivate Guard can return Observable<boolean | UrlTree> CanActivate Guard可以返回Observable<boolean | UrlTree> Observable<boolean | UrlTree> as of version 7.1.0 of Angular. Observable<boolean | UrlTree>从Angular的7.1.0版本开始。 This allows for the guard to evaluate a boolean and return a route redirect if it evaluates to false. 这使防护程序可以评估boolean ,如果评估结果为false,则返回路由重定向。

I wrote an answer a few days ago which goes over the implementation details but here's how I would approach it in your case: 我几天前写了一个答案 ,它详细介绍了实现细节,但是在您的情况下,我将按照以下方法进行处理:

First, create the guard: 首先,创建防护:

import { Injectable } from '@angular/core';
import { CanActivate, Router, UrlTree, ActivatedRouteSnapshot } from '@angular/router';
import { Observable, of } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
// import your TaskService here

@Injectable()
export class RequireDocumentsGuard implements CanActivate {
  constructor(private router: Router, private taskService: TaskService) {
  }

  canActivate(route: ActivatedRouteSnapshot): Observable<boolean | UrlTree> {
    return this.taskService.DocumentsTask(route.params.taskId).pipe(
      catchError(() => of(false)),
      map(documents => !!documents || this.router.parseUrl('error/404'))
    );
  }
}

Then simply add the guard to your route in your RoutingModule : 然后只需在RoutingModule防护添加到您的路由RoutingModule

{ 
  path: 'your/desired/route/:taskId', 
  component: YourComponentHere,
  canActivate: [RequireDocumentsGuard ]
}

Note: You have not posted your route structure so I will also make the point that you need to ensure that your 'error/404' route actually exists like: 注意:您尚未发布路由结构,因此我也要指出,您需要确保“ error / 404”路由确实存在,例如:

{ 
  path: 'error/404', 
  component: Your404ErrorComponent
}

Hope that helps! 希望有帮助!

use Auth Guard like this: 像这样使用Auth Guard:

ng generate guard auth  

import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from 
'@angular/router';
import { Observable } from 'rxjs/Observable';
import { AuthService } from './auth.service';
import {Router} from '@angular/router';
@Injectable()
export class AuthGuard implements CanActivate {
  constructor(private auth: AuthService,
    private myRoute: Router){
  }
  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {

    const taskId = next.params.taskId;
    if (taskId) {
      return true;
    }else{
      this.myRoute.navigate(["/error/404"]);
      return false;
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM