Angular 9 : Error NG2003: No suitable injection token for parameter 'url' of class 'DataService' . 找到字符串

标签 angular dependency-injection angular-services

在处理我遇到问题的程序时,构造函数及其子类的依赖注入(inject)。

  • DataService:一个服务类,它在一个地方拥有所有的 CRUD 操作,并且还有一个参数化的构造函数,用于从其子类中注入(inject)端点 URL 和 HTTP。
  • constructor(private url: string, private http: Http) { }
    
  • PostService : 一个服务类,它扩展了上述 DataService 类以在其中具有 CRUD 操作功能和一个参数化的构造函数,并在内部调用 super(endPointURL, httpObject),如下所示:
  • constructor(http: Http) {
        super('https://jsonplaceholder.typicode.com/posts', http);
    }
    
    在此重构之前(移动所有常见的 CRUD 操作并通过子类对其进行扩展),我的代码按预期工作,但 之后 以上更改我收到以下错误:
    Date: 2020-03-22T15:26:23.248Z - Hash: 7130497a38c152c58258
    5 unchanged chunks
    
    Time: 1859ms
    
    ERROR in src/app/services/data.service.ts:14:23 - error NG2003: No suitable injection token for parameter 'url' of class 'DataService'.
    Found string
    
    14   constructor(private url: string, private http: Http) { }
    
    此外,当我从 Datsource 构造函数中删除 url 参数(相应地修改 PostService.ts)时,api 正在按预期工作。不知道为什么!!!
    我在用:
    Angular CLI:9.0.4
    节点:12.16.1
    操作系统:win32 x64
    Angular :
    ...
    Ivy 工作区:
    包版本
    @angular-devkit/架构师 0.900.4
    @angular-devkit/核心 9.0.4
    @angular-devkit/schematics 9.0.4
    @原理图/Angular 9.0.4
    @原理图/更新 0.900.4
    rxjs 6.5.3
    数据服务.ts
    import { Injectable } from '@angular/core';
    import { Http } from '@angular/http';
    import { throwError } from 'rxjs';
    import { catchError } from 'rxjs/operators';
    import { AppError } from '../common/app-error';
    import { NotFoundError } from '../common/not-found-error';
    import { BadInput } from '../common/bad-input';
    
    @Injectable()
    export class DataService {
    
    //   private url = 'https://jsonplaceholder.typicode.com/posts';
    
      constructor(private url: string, private http: Http) { }
    
      getAll() {
        return this.http.get(this.url).pipe(catchError(this.errorHandle));
      } 
    
      create(resource) {
        return this.http.post(this.url, JSON.stringify(resource))
          .pipe(catchError(this.errorHandle));
      }
    
      update(resource) {
        return this.http.patch(this.url + '/' + resource.id, JSON.stringify({ isRead: true }))
        .pipe(catchError(this.errorHandle));
      }
    
      delete(resource) {
        return this.http.delete(this.url + '/' + resource.id)
          .pipe(catchError(this.errorHandle));
      }
      
      private errorHandle(error: Response){
        if (error.status === 404) {
          return throwError(new NotFoundError());
        }
        if (error.status === 400) {
          return throwError(new BadInput(error.json()));
        }
        return throwError(new AppError(error));
      }
    
    }
    
    PostService.ts
    import { Injectable } from '@angular/core';
    import { Http } from '@angular/http';
    import { DataService } from './data.service';
    
    @Injectable()
    export class PostService extends DataService {
    
      constructor(http: Http) {
        super('https://jsonplaceholder.typicode.com/posts', http);
      }
    
    }
    

    最佳答案

    数据服务.ts :
    更新构造函数。

    //import { Inject } from '@angular/core';
    
    constructor(@Inject(String) private url: string, private http: Http)
    
    更新(解释):
    根据https://angular-2-training-book.rangle.io/di/angular2/inject_and_injectable ;@Inject()是一种手动机制,用于让 Angular 知道必须注入(inject)参数。@Inject装饰器仅用于注入(inject)原语。
    原始类型 是数字、字符串、 bool 值、大整数、符号、空值、未定义。
    可以使用的另一种( 替代 )方式是:
    //import { Inject } from '@angular/core';
    
    @Inject('url') private url: string;
    

    关于Angular 9 : Error NG2003: No suitable injection token for parameter 'url' of class 'DataService' . 找到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60801513/

    相关文章:

    angular - 如果找不到数据,则解析 Guard : Proper way to resolve observable from Routes,

    c# - 我可以在 Controller 的构造函数中访问 User.Identity.Name 吗?如果不是,最佳实践是什么?

    java - Servlet 不应该有可变的实例字段误报 Spring Autowiring (squid :S2226)

    c# - 是否可以在非静态引用的程序集中的类型上调用 Activator.CreateInstance?

    Angular 文件夹结构和组件服务

    javascript - 预期响应包含一个对象,但得到一个用于 GET 操作的数组

    angular - 如何使用 angular-cli 和 ng-bootstrap 自定义 Bootstrap 4 SCSS 变量?

    Angular 如何将数据从指令传递到父组件

    javascript - 更改全屏 html5 视频的尺寸

    javascript - 如何在工厂服务中定义的ng-click中调用函数