Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度,在rooter导航后显示成功警报_Angular_Typescript_Bootstrap 4 - Fatal编程技术网

Angular 角度,在rooter导航后显示成功警报

Angular 角度,在rooter导航后显示成功警报,angular,typescript,bootstrap-4,Angular,Typescript,Bootstrap 4,我试图找到一种方法,在我成功填写表格并重定向到另一个页面后,弹出消息或发出警报 导航后类似于“新表单已成功添加” 这是我的rooter导航代码 specialEvents = [] constructor( private _eventService: EventService, private _router: Router) { } ngOnInit(): void { this._eventService.getSpecialEvents()

我试图找到一种方法,在我成功填写表格并重定向到另一个页面后,弹出消息或发出警报

导航后类似于“新表单已成功添加”

这是我的rooter导航代码

specialEvents = []

  constructor(    private _eventService: EventService,
    private _router: Router) { }

  ngOnInit(): void {
    this._eventService.getSpecialEvents()
      .subscribe(
        res => this.specialEvents = res,
        err => {
          if( err instanceof HttpErrorResponse ) {
            if (err.status === 401) {
              this._router.navigate(['/home'])
            }
          }
        }
      )
  }


任何人都可以告诉我如何在之后显示弹出窗口或成功警报。_router.navigate(['/home'])是的,您可以设置超时:

  ngOnInit(): void {
    this._eventService.getSpecialEvents()
      .subscribe(
        res => this.specialEvents = res,
        err => {
          if( err instanceof HttpErrorResponse ) {
            if (err.status === 401) {
              this._router.navigate(['/home'])

        setTimeout(() => {
          window.alert('Unauthorized');
          }, 500);
            }

          }
        }
      )
  }

您需要在链接到路由“/home”的组件的ngOnInit方法中添加警报弹出事件

例如: 如果HomeComponent链接到应用程序路由器中的路径“/home”,则

{路径:'home',组件:HomeComponent}


然后,在路由器导航到“/主页”期间,将启动组件“HomeComponent”。因此,在“HomeComponent”的ngOnInit方法中,添加警报弹出功能以显示警报消息。

您有很多选择

  • 导航之后,您可以使用乐观的方法来呼叫您的toast或alert:
  • 您可以订阅路由器事件,当导航从您的组件开始并在主组件上结束时,您可以在那里抛出警报
  • 应用程序组件中
    可以添加以下内容:

    this.router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe((event: NavigationEnd) => {
     if (event.url.inlcudes('home')) alert();
    });
    
  • 您可以使用路由器的导航承诺链来处理组件中的导航:
  • 附言:如果你想知道,如何创建一个自定义烤面包机?我建议您首先使用第三方库。是个不错的选择。

    试试这个

    在app.ts文件中添加此

     ngAfterViewInit() {
    
         setTimeout(() => {
           if (this.router.url === '/home') {
             window.alert('welcome');
           }
         },1000)
      }
    

    谢谢,这就是我想要的
    this.router.navigate(['/', 'home'])
        .then(nav => {
           window.alert('Home');
        }, err => {
          console.log(err) // when there's an error
        });
    
     ngAfterViewInit() {
    
         setTimeout(() => {
           if (this.router.url === '/home') {
             window.alert('welcome');
           }
         },1000)
      }