Angular 角度5。为什么提供程序需要组件。注入错误

Angular 角度5。为什么提供程序需要组件。注入错误,angular,dependency-injection,angular5,Angular,Dependency Injection,Angular5,提供了一项小型服务,在授权和注销用户时更新某些数据的显示(显示和隐藏菜单项和图片) 代码 但是当我尝试登录时,我得到了一个错误 NullInjectorError: No provider for FilmComponent! 这里有一个到github的链接,在那里你可以更清楚地看到整个项目 我不明白。我通过类构造函数在服务中使用相同的组件 在Angular(或Typescript)中,这不是您所使用的方式 constructor()用于依赖项注入。您的UpdateDataService是一个

提供了一项小型服务,在授权和注销用户时更新某些数据的显示(显示和隐藏菜单项和图片)

代码

但是当我尝试登录时,我得到了一个错误

NullInjectorError: No provider for FilmComponent!
这里有一个到github的链接,在那里你可以更清楚地看到整个项目 我不明白。我通过类构造函数在服务中使用相同的组件


在Angular(或Typescript)中,这不是您所使用的方式

constructor()
用于依赖项注入。您的UpdateDataService是一个单例的、注入到应用程序根组件中的组件。注入时,**薄膜组件**可能未初始化

更好地将该服务注入到组件和服务中,并在函数中更改值时发出值的使用主体或行为主体

在职:

  surUsrAvtr = new BehaviourSubject<any>();
  showElse = new BehaviourSubject<any>();

  updateWhenAuthorized() {
    console.log('user_authorized');
    this.app.current_user_avatar = this.user.getUSer().Avatar;
    this.app.user_authorized = true;
    this.app.show_favorite = true;
    this.app.IsAutentification = false;
    this.surUsrAvtr.next(this.user.getUSer().Avatar);
    this.showElse.next(false);
  }

  updateWhenLogout() {
    console.log('user_logout');
    this.app.user_authorized = false;
    this.app.show_favorite = false;
    this.showElse.next(true);
  }

这同样适用于AppComponent,这与Angular(或Typescript)中的方式不同

constructor()
用于依赖项注入。您的UpdateDataService是一个单例的、注入到应用程序根组件中的组件。注入时,**薄膜组件**可能未初始化

更好地将该服务注入到组件和服务中,并在函数中更改值时发出值的使用主体或行为主体

在职:

  surUsrAvtr = new BehaviourSubject<any>();
  showElse = new BehaviourSubject<any>();

  updateWhenAuthorized() {
    console.log('user_authorized');
    this.app.current_user_avatar = this.user.getUSer().Avatar;
    this.app.user_authorized = true;
    this.app.show_favorite = true;
    this.app.IsAutentification = false;
    this.surUsrAvtr.next(this.user.getUSer().Avatar);
    this.showElse.next(false);
  }

  updateWhenLogout() {
    console.log('user_logout');
    this.app.user_authorized = false;
    this.app.show_favorite = false;
    this.showElse.next(true);
  }

这同样适用于AppComponent,这里有两个问题:

第一:

您正在向服务中注入组件,这是没有意义的。我从您的代码中了解到,需要从服务中更新多个组件。那么为什么不使用主题或行为主题之类的东西呢?您的服务将发出带有
主题的新值。接下来
和组件将订阅这些事件。请看一个简单的例子

第二:

要在angular中使用服务,必须将其添加到使用该服务的模块或组件的providers数组中。()

我已经调查了Github回购协议。链接并看到您分别向每个组件提供服务,这意味着每个组件将接收一个单独的服务实例。那可能会给你带来麻烦。
您可能希望将服务添加到模块的提供者数组中。这样,当注入时,该模块的所有组件都将接收该服务的单例实例。请参见此处的两个问题中的更好解释:

第一:

您正在向服务中注入组件,这是没有意义的。我从您的代码中了解到,需要从服务中更新多个组件。那么为什么不使用主题或行为主题之类的东西呢?您的服务将发出带有
主题的新值。接下来
和组件将订阅这些事件。请看一个简单的例子

第二:

要在angular中使用服务,必须将其添加到使用该服务的模块或组件的providers数组中。()

我已经调查了Github回购协议。链接并看到您分别向每个组件提供服务,这意味着每个组件将接收一个单独的服务实例。那可能会给你带来麻烦。
您可能希望将服务添加到模块的提供者数组中。这样,当注入时,该模块的所有组件都将接收该服务的单例实例。请参阅

中的更好解释,您为什么将
FilmComponent
作为DI注入?它是一个组件,不是一个服务。我从未见过以这种方式注入组件。为什么要将
FilmComponent
作为DI注入?它是一个组件,不是一个服务。我从未见过以这种方式注入组件。
export class FilmComponent implements OnInit {

  urrent_user_avatar: any;
  showElseNeedLoginForCommentBlock: any;

  constructor(private upDataService: UpdateDataService) {}

  ngOnInit() {
    this.upDataService.surUsrAvtr.subscribe(_ => this.urrent_user_avatar = _);
    this.upDataService.showElse.subscribe(_ => this.showElseNeedLoginForCommentBlock = _);
  }
}