Angular 2/4 How to get route parameters in app component?

前端 未结 3 474
一向
一向 2021-02-12 10:34

As I am new to angular 2/4 I am having trouble setting up a new application as per my need.

I am trying to build an application which will be called for from some anothe

3条回答
  •  醉酒成梦
    2021-02-12 11:05

    import { Component, OnInit, OnDestroy } from '@angular/core';
    import {  Router, ActivatedRoute, Params, RoutesRecognized  } from '@angular/router';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.scss']
    })
    export class AppComponent implements OnInit {
    
      constructor( private route: ActivatedRoute, private router: Router ) {}
    
      ngOnInit(): void {
        this.router.events.subscribe(val => {
           if (val instanceof RoutesRecognized) {
             if (val.state.root.firstChild.params.id) {
              localStorage.setItem('key1', val.state.root.firstChild.params.id);
              localStorage.setItem('key2', val.state.root.firstChild.params.id2);
             }
                console.log('test', val.state.root.firstChild.params);
            }
        });
    
    }
    
    }
    

提交回复
热议问题