zoukankan      html  css  js  c++  java
  • [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 

  • 相关阅读:
    BZOJ1511: [POI2006]OKR-Periods of Words
    BZOJ1009: [HNOI2008]GT考试
    BZOJ1355: [Baltic2009]Radio Transmission
    BZOJ1415: [Noi2005]聪聪和可可
    BZOJ1004: [HNOI2008]Cards
    UVA11077 Find the Permutations
    LA3641 Leonardo's Notebook
    UVA10294 Arif in Dhaka
    UVA11762 Race to 1
    UVA11427 Expect the Expected
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9000731.html
Copyright © 2011-2022 走看看