How to handle query parameters in angular 2

后端 未结 12 783
灰色年华
灰色年华 2020-11-27 03:41

In my routable component I have

@RouteConfig {
  {path: \'/login\',   name: \'Login\', component: LoginComponent}
}  

But how

12条回答
  •  隐瞒了意图╮
    2020-11-27 04:20

    The simple way to do that in Angular 7+ is to:

    Define a path in your ?-routing.module.ts

    { path: '/yourpage', component: component-name }
    

    Import the ActivateRoute and Router module in your component and inject them in the constructor

    contructor(private route: ActivateRoute, private router: Router){ ... }
    

    Subscribe the ActivateRoute to the ngOnInit

    ngOnInit() {
    
        this.route.queryParams.subscribe(params => {
          console.log(params);
          // {page: '2' }
        })
    }
    

    Provide it to a link:

    2
    

提交回复
热议问题