[英]How to add id to url for Http get request with Angular
我在angular2服务中有一个函数,该函数传递了一个id附加到Http get请求的基本URL上,但是它不起作用。 当我输入确切的字符串时,它可以很好地获取数据,但它不会让我附加变量,例如
' http:// localhost:3000 / project / allTask / '+ project_id
getTasks(project_id){
this.loadToken();
let headers = new Headers();
headers.append('Content-Type','application/json');
return this.http.get('http://localhost:3000/project/allTask/5b0919ea256f2e15f8f4364f' , {headers: headers})
.map(res => res.json());
}
更新:
我相信在传递undefined时的project_id。 这是我组件的功能。
ngOnInit() {
this.authService.getProfile().subscribe(profile => {
this.user_id = profile.user._id;
console.log(this.user_id); //RETURNS CORRECT ID HERE
},
err =>{
console.log(err);
return false;
});
console.log(this.user_id);//UNDEFINED
this.authService.getProjects(this.user_id).subscribe(data => {
this.projects = data;
},
err =>{
console.log(err);
return false;
});
}
像这样使用它:
getTasks(project_id){
this.loadToken();
let headers = new Headers();
headers.append('Content-Type','application/json');
return this.http.get('http://localhost:3000/project/allTask/' + project_id, {headers: headers})
.map(res => res.json());
}
要么
getTasks(project_id){
this.loadToken();
let headers = new Headers();
headers.append('Content-Type','application/json');
return this.http.get(`http://localhost:3000/project/allTask/${project_id}`, {headers: headers})
.map(res => res.json());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.