How to use ActivatedRoute in Angular 5?

前端 未结 5 481
-上瘾入骨i
-上瘾入骨i 2020-12-16 19:26

I am trying to do exactly the same thing as in this post: Angular 4 get queryString

I am using Angular 5.2.5.

ActivatedRoute seems to be the thing to use to

5条回答
  •  隐瞒了意图╮
    2020-12-16 19:56

    You need to import ActivatedRoute from @angular/router like

    import { ActivatedRoute } from '@angular/router';
    

    then add this line to the imports array of the @NgModule in app.module.ts:

    imports:[
            ........,
            RouterModule.forRoot()
    ],
    

    then you can use any where as below:

    constructor(private route: ActivatedRoute) {
         console.log(route.snapshot.queryParamMap); // this
    }
    // or
    queryString : string;
    getQueryString(){
       this.queryString = this.route.queryParamMap.get('myQueryParam');
    }
    

    No. You don't need app.routing.ts if you don't have to navigate pages within your app.

提交回复
热议问题