简体   繁体   English

在 router.navigate 之后防止 Angular 组件渲染

[英]Prevent Angular component rendering after router.navigate

If to execute this.router.navigate(['someRoute']) inside ngOnInit , initial component still being rendered before redirection.如果在 ngOnInit 中执行 this.router.navigate([' ngOnInit this.router.navigate(['someRoute']) ,初始组件仍然在重定向之前被渲染。

For example I have some component:例如我有一些组件:

@Component({
  selector: 'my-app',
  template: '<child-comp></child-comp>',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  constructor(private router: Router) {
  }

  ngOnInit() {
    console.log('ngOnInit from AppComponent');
    this.router.navigate(['another']);
  }
}

It contains child in template:它在模板中包含子项:

@Component({
  selector: 'child-comp',
  template: '<div>CHILD<div>'
})
export class ChildComponent  {
  ngOnInit() {
    console.log('Should NOT be called (ChildComponent)');
  }
}

And before view initialization (inside ngOnInit hook) I perform redirection to another component:在视图初始化之前(在ngOnInit钩子内),我执行重定向到另一个组件:

@Component({
  selector: 'another-comp',
  template: '<div>Another<div>'
})
export class AnotherComponent  {
  ngOnInit() {
    console.log('Should be called (AppComponent)');
  }
}

I expect that child component will not be initialized, as it happens after redirection call, but it's ngOnInit() hook is still gets triggered.我希望子组件不会被初始化,因为它发生在重定向调用之后,但它的ngOnInit()钩子仍然被触发。

How to prevent child component initialization in this case?在这种情况下如何防止子组件初始化?

Stackblitz: https://stackblitz.com/edit/angular-gjunnr?file=src%2Fapp%2Fanother%2Fanother.component.ts Stackblitz: https://stackblitz.com/edit/angular-gjunnr?file=src%2Fapp%2Fanother%2Fanother.component.ts

PS I didn't find the way to prevent component rendering and end up with modifying design. PS我没有找到阻止组件渲染并最终修改设计的方法。

This is what guards are for:这就是警卫的用途:

Create a guard that determines if the user is allowed to go to that page:创建一个守卫,确定是否允许用户 go 到该页面:

@Injectable({
  providedIn: 'root'
})
export class PreventNavigateGuard implements CanActivate {
  constructor(public router: Router) {}
  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ):
    | Observable<boolean | UrlTree>
    | Promise<boolean | UrlTree>
    | boolean
    | UrlTree {
    return this.router.createUrlTree(["another"]);
  }
}

Then in your routes use canActivate to guard it:然后在您的路线中使用 canActivate 来保护它:

const appRoutes: Routes = [
  { path: '', component: AppComponent, canActivate: [PreventNavigateGuard] },
  { path: 'another', component: AnotherComponent },
];

https://angular.io/guide/router#milestone-5-route-guards https://angular.io/guide/router#milestone-5-route-guards

Here is a stackblitz: https://stackblitz.com/edit/angular-kbzx1v这是一个堆栈闪电战: https://stackblitz.com/edit/angular-kbzx1v

Alternatively, you can just apply an ngIf to the child component and only have it render when a property in the app.component is true.或者,您可以只将ngIf应用于子组件,并且仅在app.component中的属性为 true 时才呈现它。

<child-comp *ngIf="false"></child-comp>

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

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