简体   繁体   English

angular 9 依赖项注入错误。 " class 不能通过依赖注入创建,因为它没有 Angular 装饰器

[英]angular 9 dependency injection error. " The class cannot be created via dependency injection, as it does not have an Angular decorator

The same code worked for me in angular 8 but now it's giving me this error.相同的代码在 angular 8 中对我有用,但现在它给了我这个错误。 "The class 'BaseService' cannot be created via dependency injection, as it does not have an Angular decorator. This will result in an error at runtime. “无法通过依赖注入创建 class ‘BaseService’,因为它没有 Angular 装饰器。这将导致运行时出错。

Either add the @Injectable() decorator to 'BaseService', or configure a different provider (such as a provider with 'useFactory')."

I'm only trying to achieve simple inheritence here.我只是想在这里实现简单的继承。

1) BaseService.ts (parent class) 1)BaseService.ts(父类)

import { environment } from 'src/environments/environment';
import { HttpHeaders } from '@angular/common/http';

export class BaseService {
    url
    constructor(postfixUrl) {
        this.url = environment.backendUrl + postfixUrl
    }

    setUpHeaders() {
       return {
           headers: new HttpHeaders({
               'Content-Type': 'application/json'
           })
       }
} 
}

2) AuthService.ts (child class) 2)AuthService.ts(子类)

import { Injectable } from "@angular/core";
import {HttpClient} from '@angular/common/http'
import { BaseService } from './base.service';

@Injectable({
    providedIn: 'root'
  })

export class AuthService extends BaseService {
    url
    constructor(private http: HttpClient) {
        super('auth')
    }
    register(user) {
        return this.http.post(this.url, user, this.setUpHeaders())
    }
} 

3) auth.module.ts 3) auth.module.ts

@NgModule({
  declarations: [
    AuthComponent,
    LoginComponent,
    RegisterComponent
  ],
  imports: [
    CommonModule,
    AuthRoutingModule,
    HttpClientModule,
    FormsModule,
    ReactiveFormsModule


  ],
  providers: [AuthService, BaseService]
})
export class AuthModule { }

the error错误

Anguler 9/10垂钓者 9/10

If you want to use a service for a single module then you do not need to use @Injectable({ providedIn: 'root' }) .如果您想为单个模块使用服务,则不需要使用@Injectable({ providedIn: 'root' })

Only two step:只有两步:

  1. @Injectable() add in your service. @Injectable()添加您的服务。
  2. providers: [ BaseService ] add in your module inner NgModule . providers: [ BaseService ]添加到你的模块内部NgModule

You have added BaseService to the module providers.您已将BaseService添加到模块提供程序。 BaseService accepts the parameter postfixUrl in its constructor. BaseService在其构造函数中接受参数postfixUrl

You should remove at least the BaseService from the providers, since Angular won't know how to resolve the parameters.您应该至少从提供程序中删除BaseService ,因为 Angular 不知道如何解析参数。

Adding a service to the module providers means that an instance of that service will be shared within that module.向模块提供者添加服务意味着该服务的实例将在该模块中共享。 Adding @Injectable({ providedIn: 'root' }) means it will be shared within the whole app.添加@Injectable({ providedIn: 'root' })意味着它将在整个应用程序中共享。

You 2 competing methods of registering services for dependency injection, and registering BaseService is invalid.你 2 种竞争注册服务的方法进行依赖注入,注册BaseService是无效的。

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

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