angular - Enjeksiyon belirtecini dışa aktarmadıkça Servis enjeksiyonu Hatası


Enjeksiyon hatası, enjeksiyon token'ını export etmediğiniz sürece

AOT paketi sonrası

Kendi oluşturduğum bir InjectionToken ile tembel yükleme özellik modülünün @NgModule dekoratöründe bir servis sağlayıcı yapılandırıyorum.

const featureServiceInjectToken = new InjectionToken('FEATURE_SERVICE')
// export const featureServiceInjectToken = new InjectionToken('FEATURE_SERVICE')

@NgModule({
    declarations: [FeatureComponent],
    imports: [
        CommonModule,
        RouterModule.forChild([
            {
                path: '',
                component: FeatureComponent,
                data: {
                    serToken: featureServiceInjectToken
                }
            }
        ])
    ],
    providers: [
        {
            provide: featureServiceInjectToken,
            useClass: FeatureService,
        }
    ]
})
export class FeatureModule { }

Oluşturulan token, komponentin yapıcı fonksiyonunda, servisi bu kendimiz tanımladığımız Token aracılığıyla almak istiyorum, route konfigürasyonunun data özelliği üzerinden componente geçirilir.

//In Component:

constructor(
    injector: Injector,
    route: ActivatedRoute,
) {
    const token = route.snapshot.data['serToken']
    this.feaSer = injector.get(token)
}

Üretim koduna derlemeden sonra bir InjectionError hatası oluştu:

NullInjectorError: StaticInjectorError(s)[InjectionToken FEATURE_SERVICE]: 
  StaticInjectorError(Platform: core)[InjectionToken FEATURE_SERVICE]: 
    NullInjectorError: No provider for InjectionToken FEATURE_SERVICE!
NullInjectorError: StaticInjectorError(s)[InjectionToken FEATURE_SERVICE]: 
  StaticInjectorError(Platform: core)[InjectionToken FEATURE_SERVICE]: 
    NullInjectorError: No provider for InjectionToken FEATURE_SERVICE!

Kodu birçok kez değiştirmeyi denedikten sonra, sorun bu InjectionToken const'u dışa aktar sayesinde çözüldü:

export const featureServiceInjectToken = new InjectionToken('FEATURE_SERVICE')

Çok garip, ben yapmadım ve bu token değişkenini başka bir ts dosyasına ithal etmeye gerek yok

Birisi bunu açıklayabilir mi?

İşte Kod:

https://stackblitz.com/github/593377931/angular-injection-demo

Angular Sürüm 7.2.0 ve 8.0.3 aynı probleme sahip.

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 8.0.6
Node: 10.16.0
OS: linux x64
Angular: 8.0.3
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.800.6
@angular-devkit/build-angular     0.800.6
@angular-devkit/build-optimizer   0.800.6
@angular-devkit/build-webpack     0.800.6
@angular-devkit/core              8.0.6
@angular-devkit/schematics        8.0.6
@angular/cli                      8.0.6
@ngtools/webpack                  8.0.6
@schematics/angular               8.0.6
@schematics/update                0.800.6
rxjs                              6.4.0
typescript                        3.4.5
webpack                           4.30.0

İlgili makaleler