简体   繁体   English

Angular 7-设置路线导航之间的时间间隔

[英]Angular 7 - Set time interval between navigation of routes

I want to see a loader while navigating from one page to another. 我想在从一页导航到另一页时看到一个加载器。 For this, I purposely want to set timeout or time interval between the navigation of two routes. 为此,我故意要设置两条路线导航之间的超时或时间间隔。 The name of routes is "list" and "register". 路由的名称为“列表”和“注册”。 How do I add a time interval between the navigation of two routes? 如何在两条路线的导航之间添加时间间隔?

Dashboard Component : 仪表板组件

import { Component, OnInit } from '@angular/core';
import { Router, Event, NavigationStart, NavigationEnd } from '@angular/router';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {
  showloader = true;

  constructor(private route: Router) {
    this.route.events.subscribe((routerEvent: Event) => {

      if(routerEvent instanceof NavigationStart){  
        this.showloader = true;
      }

      if(routerEvent instanceof NavigationEnd){
        this.showloader = false;
      }
    });
  }

  ngOnInit() {
  }
}

DashBoard.html: DashBoard.html:

<div>
    <nav class="navbar navbar-expand-sm bg-light">
        <ul class="nav navbar-nav">
            <li class="nav-item">
                <a class="nav-link" routerLink="list">List of Animes</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" routerLink="register">Register</a>
            </li>
            <li class="nav-item">
                <a class="nav-link">Description</a>
            </li>
        </ul>
    </nav>
</div>
routerOnActivate() { //or ngOnInit()
  this.timer = setInterval(()=>{
                ...
            }, 100);
}

routerOnDeactivate() {
  clearInterval(this.timer);
}

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

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