简体   繁体   中英

How to receive current routes response in angular CLI?

Using angualar cli and nodejs for single app, passportjs configured in nodejs, node gives user data and returns Object with angular port, how to receive this response in angular routes at ngOnInit() ?

Redirect URL : http://localhost:4200/api/pauth?code=4%2FAAAOF7UrUC...

I have same route in angular too /api/pauth to access data.

Response for above URL (from nodejs):

{"status":"0","message":"Looged in successfully","user":{"_id":"5a797783cb91253f8c667232","email":"example@gmail.com","password":"ya9.GltZBYvinL7N6D6OvgQ1z2Poa7ceOO3TS7LQB-CcsT3","authType":"google","createdAt":"2018-02-06T09:38:11.965Z","updatedAt":"2018-02-06T09:38:11.965Z","__v":0}}

component.ts:

constructor(private http:Http, private router:Router,private httpc: HttpClient) { }

ngOnInit() {
    console.log(this.router.url);
    this.http.get(this.router.url).subscribe(res => {
      this.posts = res;
      console.log(this.posts);
    });
}

i will tell solution with httpclient and i hope you imported httpclient module in app.module.ts

constructor(private http: HttpClient) {
   let headers = new HttpHeaders({'Content-Type': 'application/json' });
   this.options =headers;
}
ngOninit(){
   this.getdata().subscribe(data=>{console.log(data)})
}
getdata(){
   this.http.get<any>(url,
     {headers:this.options,responseType:"json",observe:"response"})
     .map(res =>res.body)
}

Alternate solution :

Object response not accessible while the two ports are mingled, need to redirect from Node JS with params, now able to handle angular routes.

Example from node - api.js :

var code = req.user._id;
var url = 'http://localhost:4200/pauth?id='+code;
res.redirect(url);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM