useFactory提供者没有在我的模块中被调用。

huangapple go评论83阅读模式
英文:

useFactory provider is not being called from my module

问题

我模块的提供者没有被 Angular 调用。我正在使用 Angular 14。

  1. export const FIRE_BASE_AUTH = new InjectionToken<Auth>('FIRE_BASE_AUTH');
  2. function getFirebaseAuth() {
  3. console.log('getFirebaseAuth called...'); // 这不会显示在控制台中
  4. return LoginSpaModule.firebaseAuth;
  5. }
  6. @NgModule({
  7. declarations: [
  8. LoginComponent
  9. ],
  10. imports: [
  11. // ...
  12. ],
  13. providers: [
  14. { provide: FIRE_BASE_AUTH, useFactory: getFirebaseAuth }
  15. ],
  16. exports: [LoginComponent]
  17. })
  18. export class LoginSpaModule {
  19. static firebaseAuth: Auth;
  20. constructor() {
  21. console.log('we are in the LoginSpaModule constructor...', LoginSpaModule.firebaseAuth); // 这在控制台中显示
  22. }
  23. }

以及我的组件:

  1. @Component({
  2. selector: 'app-login',
  3. templateUrl: './login.component.html',
  4. styleUrls: ['./login.component.scss']
  5. })
  6. export class LoginComponent implements OnInit {
  7. constructor(
  8. @Inject('FIRE_BASE_AUTH') private auth: Auth,
  9. private router: Router
  10. ) { }
  11. // ...
  12. }

如果我尝试将 FIRE_BASE_AUTH 注入到组件中,我会得到 NullInjectorError: No provider for FIRE_BASE_AUTH!。如果我尝试其他 provide 方法,比如 useValue,也会出现这个错误。

英文:

The provider for my module is not being called by Angular. I'm using Angular 14.

  1. export const FIRE_BASE_AUTH = new InjectionToken&lt;Auth&gt;(&#39;FIRE_BASE_AUTH&#39;);
  2. function getFirebaseAuth() {
  3. console.log(&#39;getFirebaseAuth called...&#39;); // This is not showing up in the console
  4. return LoginSpaModule.firebaseAuth;
  5. }
  6. @NgModule({
  7. declarations: [
  8. LoginComponent
  9. ],
  10. imports: [
  11. ...
  12. ],
  13. providers: [
  14. { provide: FIRE_BASE_AUTH, useFactory: getFirebaseAuth }
  15. ],
  16. exports: [LoginComponent]
  17. })
  18. export class LoginSpaModule {
  19. static firebaseAuth: Auth;
  20. constructor() {
  21. console.log(&#39;we are in the LoginSpaModule constructor...&#39;, LoginSpaModule.firebaseAuth); // This is in the console
  22. }
  23. }

And my component:

  1. @Component({
  2. selector: &#39;app-login&#39;,
  3. templateUrl: &#39;./login.component.html&#39;,
  4. styleUrls: [&#39;./login.component.scss&#39;]
  5. })
  6. export class LoginComponent implements OnInit {
  7. constructor(
  8. @Inject(&#39;FIRE_BASE_AUTH&#39;) private auth: Auth,
  9. private router: Router
  10. ) { }
  11. ...

If I try to inject the FIRE_BASE_AUTH into a component, I get NullInjectorError: No provider for FIRE_BASE_AUTH!. I get this also if I try other provide method like useValue.

答案1

得分: 1

以下是翻译好的部分:

The string would work as the provider if its a string in the @Inject as well.
{ provide: 'FIRE_BASE_AUTH', ... }
constructor(@Inject('FIRE_BASE_AUTH') ...) { } The token is not being used in this case.

英文:

The string would work as the provider if its a string in the @Inject as well.
{ provide: &#39;FIRE_BASE_AUTH&#39;, ... }
constructor(@Inject(&#39;FIRE_BASE_AUTH&#39;) ...) { } The token is not being used in this case.

huangapple
  • 本文由 发表于 2023年7月11日 04:01:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定