Angular 2 Check if request parameter exists in url

I need to check if the current url has a query string.

URL may be

http://localhost:4200/test?id=55555

or

http://localhost:4200/test

I used

this.route.queryParams
     .filter(params => 'id' in params)
     .map(params => params.id)
     .distinctUntilChanged()
     .subscribe(
     id=> {
                //check lead Id here
                console.log(id);
            }
     );

But this only works when there is an identifier in the URL. Not sure how to check if it exists.

+9
source share
6 answers

You can directly test it inside your subscription function, as shown below:

this.route.queryParams.subscribe((params)=> {
  //check lead Id here
  if(params['id']){
    console.log(params['id']);
  } else {
    console.log('id not found in params')
  }
});
+7
source

Preliminary

queryParams, , params , , , . console.log(params);. :

url: http://example.com/?lion=10&lion=15&cat=20

, :

this.route.queryParams.subscribe((params) => {
    console.log(params);
});

, lion cat, - , cat - :

{ lion: [10, 15], cat:20 }

,

url : http://example.com/?id&name=james

, id . , URL- , , , . :

if(params['id']){
    // do something
}

false, . , - , - , . lodash, /. lodash . _. :

// check if id key is present in params object first
if(_.has(params, 'id')) {
    if(params['id']=="") {
      // id key is present but has no value so do something
    } else {
      // id key is present and has a value as well so do something else
    }
} else {
    // since id key itself is not present do something totally different here
}

, , id url. .

0

, Angular . :

constructor(private activatedRoute: ActivatedRoute)

, , ngOnInit():

ngOnInit() {
    console.log("Hello ngOnInit");
    let token = null;
    this.activatedRoute.queryParamMap.subscribe((next : ParamMap) => {
      token = next.get("token")

      if (token) {
        console.log(token);
      } else {
        console.log("no token given in url");
      }
    });
  }
0

snapshot, . , URL- (, ), , . , :

constructor(private route: ActivatedRoute) {

}

ngOnInit(): void { 
    if (this.route.snapshot.params['id']) {
        // id exists, so do something with it.

    }
}

:

: . paramMap, , . paramMap.

: :

0

:

this.route.queryParams.subscribe(
    (params: Params) => {
        if (params.hasOwnProperty('id')) { console.log(params['id']); }
    }
);
0

, :

ngOnInit() {
 if (this.route.snapshot.queryParams['id']) {
      //do your stuff. example: console.log('id: ', this.route.snapshot.queryParams['id']);
 }
}

would be enough. Since you only need to check whether it exists or not. If this happens, your things will happen, for example, adding a boolean value to check if it was installed or not. If this does not exist, then nothing will happen. Then, if you need to do something somewhere else in the component, you can check the boolean later if it is true / false, depending on how you set it earlier.

0
source

Source: https://habr.com/ru/post/1666336/


All Articles