Obtain Angular Events Fired During Navigation To A Page

I would like to obtain a list of all Angular router events which were fired during the navigation to a particular view. Yes, I understand in most cases this will be the same.

I know how to subscribe to router events. So my code would look like something like this?

router.events
  .pipe(
    // toArray() maybe?
    takeUntil(),  // NavigationEnd, NavigationCancel, NavigationError
  )
  .subscribe();

This is probably just basic rxjs and my mind is bypassing an obvious answer.

3

Fist create a service property that stores the array, this array will store the events.

Then in app component listen for the events and push it into the array.

router.events
  .subscribe((event: any) => this.someService.eventArr.push(event));

Then when you navigate to the route, reset the array to empty and then trigger the navigation.

When you access the array on the component, you will have the full list.

With your approach, it gets called only on component load (A lot of events are lost), so it wont be the full list, this will provide the full list of events.

Router events observable does not complete, it continuously provides values, so unsubscribing on component destroy is essential.

Basically, you are on the right track. If you want to filter out a few router events, you can use filter() operator and then you can use toArray(). The takeUntil() can be used to stop the subscription of router events.

As Naren mentioned above, they do no stop, you need to manually stop it or filter it based on your needs.

This is a sample of how you can do that:

import { NavigationEnd, NavigationCancel, NavigationError, Event } from '@angular/router';
import { filter, takeUntil, toArray } from 'rxjs/operators';
import { Subject } from 'rxjs';

// Create a subject that emits when the navigation ends
const navigationEnd = new Subject<Event>();

router.events.pipe(
  
  // Filter for the router events you're interested in
  filter(
    event => 
      event instanceof NavigationEnd || 
    event instanceof NavigationCancel || 
    event instanceof NavigationError
  ),

  // Gather all the events into an array
  toArray(),
  
  // Complete the observable when a NavigationEnd, NavigationCancel, or NavigationError event occurs
  takeUntil(navigationEnd)
).subscribe(events => {
  console.log(events);  // This will log an array of all the router events
});

// In your navigation end logic
router.events.pipe(
  filter(event => event instanceof NavigationEnd || event instanceof NavigationCancel || event instanceof NavigationError),
).subscribe(event => {
  navigationEnd.next(event);
});

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật