Warning: Can't resolve all parameters for UsersPermissionsService This will become an error in Angular v5.x

Viewed 9551

When I am running ng build -prod I am getting the following warning.

Warning: Can't resolve all parameters for UsersPermissionsService in C:/SourceControl/Test.Client/src/app/shared/users/users-permissions.service.ts: (?, ?, ?). This will become an error in Angular v5.x
Warning: Can't resolve all parameters for UsersPermissionsService in C:/SourceControl/Test.Client/src/app/shared/users/users-permissions.service.ts: (?, ?, ?). This will become an error in Angular v5.x

My code is the following:

import { Injectable } from '@angular/core';

@Injectable()
export class UsersPermissionsService {

    public USERS_CREATE_PERMISSION: string = '';
    public USERS_UPDATE_PERMISSION: string = '';
    public USERS_DELETE_PERMISSION: string = '';

    constructor(public UsersCreatePermission: string,
        public UsersUpdatePermission: string,
        public UsersDeletePermission: string) {
        this.USERS_CREATE_PERMISSION = UsersCreatePermission;
        this.USERS_UPDATE_PERMISSION = UsersUpdatePermission;
        this.USERS_DELETE_PERMISSION = UsersDeletePermission;
    }
}


@Injectable()
export class UserModulePermissionsService extends UserPermissionsService {
    constructor() {
        super("ClientsCreate",
            "ClientsEdit",
            "ClientsDelete");
    }
}

@Component({
    templateUrl: './users-permissions.component.html',    
    providers: [UsersPermissionsService]
})
export class UsersPermissionsComponent {
    constructor(public usersPermissionsService: UsersPermissionsService) {

    }
}

and in my lazy loaded module I have:

 providers: [
        { provide: UsersPermissionsService, useClass: UserModulePermissionsService }
    ]

Now that angular 5 is out I will need to update and as the message says this warning will become an error.

Don't understand what is really the problem here.

3 Answers

Remove the @Injectable decorator from the base class. You should only include that decorator (or any decorator for that matter) on classes that Angular should instantiate directly (and while doing that also resolve their constructor parameters using the injector).

Since it is obvious that in your case the base class is not to be instantiated by angular directly (it has constructor parameters which are not known by the injector), you should remove the decorators from it.

If some one is facing this issue while building angular app, below steps worked for me :

1) Notice the error while running build command, example sample error is something like

2) Now check the same component's constructor service injections constructor service injections

3) Now go inside each service and make sure you remove default export my case I had default export here

4) Now you need to change in all places wherever this service is imported. You need to import named export with curly braces like { AlertService } instead of AlertService

Got this because I tried to add a parameter to a Service's parent

A
   constructor(logger: loggerService, someData: myData)

B extends A
    constructor (logger: loggerService, new myData())

Solution for me was find another way to set someData...

Related