无法解析 UserServiceService 的所有参数:(?)

Can't resolve all parameters for UserServiceService: (?)

我正在尝试通过 getsUserUrl 中给出的 link 获取用户数据。为此 它需要我在 header 中添加的 token(参见 token 变量)。 .每当我启动服务器时,我都会收到这样的错误。

Can't resolve all parameters for UserServiceService: (?).at syntaxError (compiler.js:1021)

这是我的代码。我不知道 Header.

中缺少什么参数
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));
}

 }

请让我知道我做错了什么。 我附上代码照片供您参考

因为有人想要我的组件代码,所以我也把它加在这里

您还需要将 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');

Error messages Can't resolve all parameters for UserServiceService: (?)表示angular DI找不到HttpClient dependency,这很可能是你没有导入里面的HttpClientModule造成的你的 AppModule.

要解决此问题,请将 @angular/common/http 中的 HttpClientModule 添加到 AppModuleimports 部分。

从您的代码中删除以下行

import 'rxjs/add/operator/map';

并在模块文件中导入 HttpClientModule。并声明到 imports 数组中。喜欢如下

import { HttpClientModule } from '@angular/common/http';

imports: [
   HttpClientModule

并在您的服务文件中删除 HttpClientModule 的导入