• [Angular] N things you might don't know about Angular Route


    Prevent Partail Page display: By using Resolver:

    @Injectable()
    export class MovieResolver implements Resolve<IMovie> {
    
      constructor(private movieService: MovieService) { }
    
      resolve(route: ActivatedRouteSnapshot,
              state: RouterStateSnapshot): Observable<IMovie> {
        const id = route.paramMap.get('id');
        return this.movieService.getMovie(+id);
      }
    }
    providers: [
       MovieService,
       MovieResolver
    ]
    {
       path: 'movies/:id',
       resolve: { movie: MovieResolver },
       component: MovieDetailComponent
    },
    ngOnInit(): void {
       this.movie = this.route.snapshot.data['movie'];
    }

    Display Loading spinner when switching page:

    constructor(private router: Router) {
       router.events.subscribe((routerEvent: Event) => {
          this.checkRouterEvent(routerEvent);
       });
    }
    
    
    checkRouterEvent(routerEvent: Event): void {
       if (routerEvent instanceof NavigationStart) {
          this.loading = true;
       }
    
       if (routerEvent instanceof NavigationEnd ||
           routerEvent instanceof NavigationCancel ||
           routerEvent instanceof NavigationError) {
          this.loading = false;
       }
    }
    <span class="glyphicon glyphicon-refresh glyphicon-spin spinner" 
          *ngIf="loading"></span>
    <router-outlet></router-outlet>

    Enable route debugging:

    RouterModule.forRoot([
      {
        path: '', component: ShellComponent,
        children: [
          { path: 'welcome', component: WelcomeComponent },
          { path: 'movies', component: MovieListComponent },
          { path: '', redirectTo: 'welcome', pathMatch: 'full' }
        ]
      },
      { path: 'login', component: LoginComponent },
      { path: '**', component: PageNotFoundComponent }
    ], { enableTracing: true })

    Talk 

  • 相关阅读:
    事件基础
    Event loop 事件的循环和为什么使用函数
    Promise 对象---异步调用
    新版博客园分类不能正常显示
    OpenCV学习
    c# 常用正则
    C#调用OpenCV——美图效果
    WPF中的Image控件Source的设置(包括直接读取图片文件的代码)
    图像处理的灰度化和二值化
    C#弹出U盘
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9000731.html
Copyright © 2020-2023  润新知