Angular 6 - Get current route and it's data

前端 未结 3 1859
离开以前
离开以前 2020-12-29 09:09

How do you able to get current route you\'re in and get\'s it\'s data, children and it\'s parent?

say if this is the route structure:

3条回答
  •  粉色の甜心
    2020-12-29 09:56

    @Component({...})
    export class CompanyComponent implements OnInit {
    
    constructor(
      private router: Router,
      private route: ActivatedRoute
    ) {}
    
    ngOnInit() {
    
      // Parent:  about 
      this.route.parent.url.subscribe(url => console.log(url[0].path));
    
      // Current Path:  company 
      this.route.url.subscribe(url => console.log(url[0].path));
    
      // Data:  { title: 'Company' } 
      this.route.data.subscribe(data => console.log(data));
    
      // Siblings
      console.log(this.router.config);
    }
    }
    

提交回复
热议问题