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

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

useFactory provider is not being called from my module

问题

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

export const FIRE_BASE_AUTH = new InjectionToken<Auth>('FIRE_BASE_AUTH');

function getFirebaseAuth() {
  console.log('getFirebaseAuth called...'); // 这不会显示在控制台中
  return LoginSpaModule.firebaseAuth;
}

@NgModule({
  declarations: [
    LoginComponent
  ],
  imports: [
    // ...
  ],
  providers: [
    { provide: FIRE_BASE_AUTH, useFactory: getFirebaseAuth }
  ],
  exports: [LoginComponent]
})

export class LoginSpaModule {
  static firebaseAuth: Auth;
  constructor() {
    console.log('we are in the LoginSpaModule constructor...', LoginSpaModule.firebaseAuth); // 这在控制台中显示
  }
 }

以及我的组件:

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {

  constructor(
    @Inject('FIRE_BASE_AUTH') private auth: Auth,
    private router: Router
  ) { }
  // ...
}

如果我尝试将 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.

export const FIRE_BASE_AUTH = new InjectionToken&lt;Auth&gt;(&#39;FIRE_BASE_AUTH&#39;);

function getFirebaseAuth() {
  console.log(&#39;getFirebaseAuth called...&#39;); // This is not showing up in the console
  return LoginSpaModule.firebaseAuth;
}

@NgModule({
  declarations: [
    LoginComponent
  ],
  imports: [
    ...
  ],
  providers: [
    { provide: FIRE_BASE_AUTH, useFactory: getFirebaseAuth }
  ],
  exports: [LoginComponent]
})

export class LoginSpaModule {
  static firebaseAuth: Auth;
  constructor() {
    console.log(&#39;we are in the LoginSpaModule constructor...&#39;, LoginSpaModule.firebaseAuth); // This is in the console
  }
 }

And my component:

@Component({
  selector: &#39;app-login&#39;,
  templateUrl: &#39;./login.component.html&#39;,
  styleUrls: [&#39;./login.component.scss&#39;]
})
export class LoginComponent implements OnInit {

  constructor(
    @Inject(&#39;FIRE_BASE_AUTH&#39;) private auth: Auth,
    private router: Router
  ) { }
...

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:

确定