我正在尝试通过getsUserUrl
中给出的链接来获取用户数据。为此
它需要我在标头中添加的token
(请参见token
变量)。每当我启动服务器时,都会出现这样的错误。
Can't resolve all parameters for UserServiceService: (?).at syntaxError (compiler.js:1021)
这是我的代码。我不知道标题中缺少什么参数。
import { Injectable,Pipe } from '@angular/core';
import { HttpHeaders, HttpClient ,HttpErrorResponse,HttpClientModule}
from "@angular/common/http";
import { catchError } from 'rxjs/operators';
import { throwError } from 'rxjs';
import 'rxjs/add/operator/map';
import { Token } from '@angular/compiler';
@Injectable({
providedIn: 'root'
})
export class UserServiceService {
usersData: any[] = [];
user:string
getUserUrl="https://auth.openshift.io/api/search/users?q=rohit";
token=`eysdsfdp.whsofd23.fdiu`;
constructor(private httpclient:HttpClient) {
}
private handleError(err: HttpErrorResponse | any) {
console.error('An error occurred', err);
return throwError(err.message || err);
}
searchUsers()
{
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.token}`
})
};
var res=this.httpclient.get(this.getUserUrl,httpOptions);
return res.pipe(catchError(this.handleError));
}
}
答案 0 :(得分:1)
错误消息Can't resolve all parameters for UserServiceService: (?)
表示角度DI找不到HttpClient
依赖性,这很可能是由于您缺少将HttpClientModule
导入AppModule
内而引起的
要解决此问题,请将HttpClientModule
中的@angular/common/http
添加到imports
的{{1}}部分。
答案 1 :(得分:1)
从代码中删除以下行
import 'rxjs/add/operator/map';
并将HttpClientModule
导入模块文件中。并声明到imports
数组中。像这样
import { HttpClientModule } from '@angular/common/http';
imports: [
HttpClientModule
并删除服务文件中的HttpClientModule
导入
答案 2 :(得分:0)
您还需要将authorization
关键字放在引号中
const httpOptions = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.token}`
});
或者您也可以用这种方式定义它
let httpOptions = new HttpHeaders();
httpOptions = httpOptions .set('h1', 'v1').set('h2','v2');