angularjs - Angular2 Component Router : Capture query parameters -
i'm struggling capture url query string parameters being passed angular2 app 3rd party api. url reads http://example.com?oauth_token=123
how can capture value of "oauth_token" inside component? i'm happily using component router basic routing it's query string. have made several attempts , latest receiving component looks like
import { component, oninit } '@angular/core'; import { activatedroute } '@angular/router'; @component({ template: '' }) export class twitterauthorisedcomponent implements oninit { private oauth_token:string; constructor(private route: activatedroute) {} ngoninit() { console.log('the oauth token is'); console.log( this.route.snapshot.params.oauth_token ); } }
any advice appreciated.
** update
if comment out routes query parameters stick however, moment include route query parameters removed on page load. below stripped down copy of routes file 1 route
import {navigationcomponent} "./navigation/components/navigation.component"; import {twitterauthorisedcomponent} "./twitter/components/twitter-authorised.component"; import { providerouter, routerconfig } '@angular/router'; export const routes: routerconfig = [ { path: '', component: twitterauthorisedcomponent } ]; export const approuterproviders = [ providerouter(routes) ];
if remove route query params stick. advice?
i can catch query prams using bellow solution.
import { router, route, activatedroute } '@angular/router'; queryparams:string; constructor(private router: router, private actroute: activatedroute) { this.queryparams= this.router.routerstate.snapshot.root.queryparams["oauth_token"]; }
using value of oauth_token queryparams. think seems fine you
if need update value queryparams, query parameter changes need add more code. below.
import { router, route, activatedroute } '@angular/router'; queryparams:string; sub:any; constructor(private router: router, private actroute: activatedroute) { this.queryparams= this.router.routerstate.snapshot.root.queryparams["oauth_token"]; } ngoninit(){ this.sub = this.router.routerstate.root.queryparams.subscribe(params => { this.queryparams = params["oauth_token"]; }); }
hope work you.
Comments
Post a Comment