typescriptangular5asp.net-core-2.0angular-cookiesngx-cookie-service

NodeInvocationException: StaticInjectorError[CookieService] - No provider for CookieService


I have an ASP.NET Core 2.0 project with an Angular 5.1.0 ClientApp (VS 2017 v15.4.5). Trying to use ngx-cookie-service.

I followed the instructions to import the ngx-cookie-service, but whenever I try DI it into a component or service constructor I get this

error:

NodeInvocationException: StaticInjectorError[CookieService]: 
StaticInjectorError[CookieService]: 
NullInjectorError: No provider for CookieService!
Error: StaticInjectorError[CookieService]: 
StaticInjectorError[CookieService]: 
NullInjectorError: No provider for CookieService!

app.module.browser.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppModuleShared } from './app.module';
import { AppComponent } from './components/app/app.component';
import { HttpModule } from '@angular/http';
import { CookieService } from 'ngx-cookie-service';

@NgModule({
    bootstrap: [AppComponent],
    imports: [BrowserModule, AppModuleShared, HttpModule,FormsModule  
    ],
    providers: [
        {
            provide: 'BASE_URL', useFactory: getBaseUrl
        },
        CookieService 
    ]
})

export class AppModule { }
[...]

setlanguage.component.ts:

[...]
import { CookieService } from 'ngx-cookie-service';

@Component({
    selector: 'setlanguage',
    templateUrl: './setlanguage.component.html'
})
export class SetLanguageComponent {
    public loc = "";
    constructor(private http: Http, @Inject('BASE_URL') private baseUrl: string, 

                private _cookieService: CookieService) // <-- DI CookieService

    { 
        this.loc = this._cookieService.get('Locale');
    }
}

packages.json

"dependencies": {
    "@angular-devkit/schematics": "0.0.40",
    "@angular/animations": "5.1.0",
    "@angular/common": "5.1.0",
    "@angular/compiler": "5.1.0",
    "@angular/compiler-cli": "5.1.0",
    "@angular/core": "5.1.0",
    "@angular/forms": "5.1.0",
    "@angular/http": "5.1.0",
    "@angular/platform-browser": "5.1.0",
    "@angular/platform-browser-dynamic": "5.1.0",
    "@angular/platform-server": "5.1.0",
    "@angular/router": "5.1.0",
    "@ngtools/webpack": "1.9.0",
    "@schematics/schematics": "0.0.10",
    "@types/webpack-env": "1.13.3",
    "angular2-template-loader": "0.6.2",
    "aspnet-prerendering": "^3.0.1",
    "aspnet-webpack": "^2.0.1",
    "awesome-typescript-loader": "3.4.1",
    "bootstrap": "3.3.7",
    "css": "2.2.1",
    "css-loader": "0.28.4",
    "es6-shim": "0.35.3",
    "event-source-polyfill": "0.0.9",
    "expose-loader": "0.7.3",
    "extract-text-webpack-plugin": "2.1.2",
    "file-loader": "0.11.2",
    "html-loader": "0.4.5",
    "isomorphic-fetch": "2.2.1",
    "jquery": "3.2.1",
    "json-loader": "0.5.4",    
    "ngx-cookie-service": "1.0.9",
    "preboot": "4.5.2",
    "raw-loader": "0.5.1",
    "reflect-metadata": "0.1.10",
    "rxjs": "5.5.5",
    "style-loader": "0.18.2",
    "to-string-loader": "1.1.5",
    "typescript": "2.6.2",
    "url-loader": "0.6.2",
    "webpack": "3.10.0",
    "webpack-hot-middleware": "2.21.0",
    "webpack-merge": "4.1.1",
    "zone.js": "0.8.18"
  },
  "devDependencies": {
    "@types/chai": "4.0.1",
    "@types/jasmine": "2.8.2",
    "chai": "4.1.2",
    "jasmine-core": "2.8.0",
    "karma": "1.7.1",
    "karma-chai": "0.1.0",
    "karma-chrome-launcher": "2.2.0",
    "karma-cli": "1.0.1",
    "karma-jasmine": "1.1.1",
    "karma-webpack": "2.0.6",
    "@angular/cli": "1.6.0"
  }

Any advice is appreciated. Thank you.


Solution

  • You need to import CookieModule in your app.module.ts

    @NgModule({
        ...
        imports: [
            CookieModule.forRoot(),
            ...
        ]
    })