Lee Maan
Lee Maan

Reputation: 719

In Angular 6 I get the error "NullInjectorError: No provider for Store" in a component

This simplified component dispatches an action to save data to the store:

import { Store } from '@ngxs/store';
import { ViewLogin } from '../actions/login.actions';

@Component({
  selector: 'app-navbar',
  templateUrl: './navbar.component.html',
  styleUrls: ['./navbar.component.css']
})

export class NavbarComponent {
  constructor(private store: Store) {}

  onSignInClick(login: string) {
    this.store.dispatch(new ViewLogin({ login }));
  }
}

It always brings about the error NullInjectorError: No provider for Store!.

The weird thing is that I have the same code in another component in the app and it's working without any problem!

Any idea?

Upvotes: 2

Views: 7480

Answers (1)

Garth Mason
Garth Mason

Reputation: 8001

With NGXS you don't need to inject the Store directly as a provider, rather import the NgxsModule into the root AppModule.

Here from the documentation:

import { NgModule } from '@angular/core';
import { NgxsModule } from '@ngxs/store';

@NgModule({
  imports: [
    NgxsModule.forRoot([
      ZooState
    ])
  ]
})
export class AppModule {}

Upvotes: 3

Related Questions