简体   繁体   English

如何在角度CLI中接收当前路由响应?

[英]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() ? 对单个应用程序使用angualar cli和nodejs,在nodejs中配置的passwordjs,node提供用户数据并返回带有角端口的Object,如何在ngOnInit()角路由中接收此响应?

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

I have same route in angular too /api/pauth to access data. 我在/api/pauth也有相同的路由来访问数据。

Response for above URL (from nodejs): 对上述URL的响应(来自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: 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 我会用httpclient告诉解决方案,希望您在app.module.ts中导入了httpclient模块

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. 当两个端口混合在一起时,对象响应不可访问,需要使用参数从Node JS重定向,现在能够处理角度路由。

Example from node - api.js : 来自节点的示例-api.js

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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