2

这个简化的组件调度一个操作来将数据保存到存储中:

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 }));
  }
}

它总是带来错误NullInjectorError: No provider for Store!

奇怪的是,我在应用程序的另一个组件中有相同的代码,而且它工作没有任何问题!

任何想法?

4

1 回答 1

3

使用 NGXS,您无需将Store直接作为提供程序注入,而是将其NgxsModule导入到 rootAppModule中。

这里来自文档:

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

@NgModule({
  imports: [
    NgxsModule.forRoot([
      ZooState
    ])
  ]
})
export class AppModule {}
于 2018-11-20T01:02:09.940 回答