Monday, 17 April 2023

How to make a method common and call from shared service file into service using angular15

i am using angular15 and here i need to generate unique id, so the process works perfectly fine, when i add set of code in that respective service file, but as that set of code is used across all service branches, i created a method under shared service and trying to get that data but i get as undefined.

shared.service.ts:

       public generateUniqueId() {
        return uuid();
       }
    
       generateCorrelationId(routerEvent:any,route:string):any {
        let previousUrl:any =[];
        let uniqueCorrelationId = '';
        routerEvent.events
        .pipe(filter(event => event instanceof NavigationEnd))
        .subscribe(({urlAfterRedirects}: NavigationEnd) => {
          previousUrl.splice(0,previousUrl.length-1)
          previousUrl = [...previousUrl, urlAfterRedirects];
          if(previousUrl.length === 1 && previousUrl[0]=== route) {
            return uniqueCorrelationId = this.generateUniqueId();
          } else if(previousUrl.length == 2 && previousUrl[1] === previousUrl[0]) {
            return uniqueCorrelationId
          } else if(previousUrl.length == 2 && previousUrl[1] != previousUrl[0]) {
            return uniqueCorrelationId = this.generateUniqueId();
          } else {
            return uniqueCorrelationId;
          }
        });
      }

component.service.ts:

    constructor(private geneateUUid:GenerateUniqueIdService) {
      let uniqueIdGeneration = this.geneateUUid.generateCorrelationId(this.router,'/findprofile');
      console.log(uniqueIdGeneration,"uniqueIdGeneration")
    }

Observation: in Console, first component console value comes and next respective console under shared service appears. also here based on particular component service hit, i am checking for generation of new key or retaining it, is it possible to do it dynamically i mean as a common checking whether previous and current routing is matching.



from How to make a method common and call from shared service file into service using angular15

No comments:

Post a Comment