Angular 4 script not working after changing route

后端 未结 6 1049
萌比男神i
萌比男神i 2020-12-11 09:59

while changing the routes to homepage, javascript and jquery code is not loading in Angular 5

6条回答
  •  一个人的身影
    2020-12-11 10:19

    here is an angular way

    export class PagesComponent implements OnInit, OnDestroy {
      routerSubscription: any;
      constructor(private router: Router) {}
    
      ngOnInit() {
        this.recallJsFuntions();
      }
    
      /**
       * @description when we navigate from one page to another `scope of js funtions`
       * finished so we have to call it again.
       */
      recallJsFuntions() {
        this.routerSubscription = this.router.events
          .pipe(filter(event => event instanceof NavigationEnd))
          .subscribe(event => {
            $.getScript('/assets/js/common.js');
          });
      }
    
      ngOnDestroy() {
        this.routerSubscription.unsubscribe();
      }
    }
    

提交回复
热议问题