如何在Angular的延迟加载模块中添加第三方服务?在TranslateService(@ ngx-translate)和OverlayModule

问题描述 投票:0回答:1

有“ Minko Gechev” Route-level code splitting in Angular sample app的分叉,带有平移和叠加注入。

import { Component, OnInit, Injectable } from '@angular/core';
import { CookieService } from "ngx-cookie-service";
import { ComponentType, Overlay, OverlayConfig } from "@angular/cdk/overlay";

import { TranslateService } from "@ngx-translate/core";

@Injectable({
  providedIn: 'root'
})
export class OverlayService {

  constructor(public overlay: Overlay) {}
}


@Component({
  selector: 'app-nyan',
  template: '<img src="/assets/nyan.png">',
  styleUrls: ['./nyan.component.css']
})
export class NyanComponent implements OnInit {

  constructor(
    private cookieService:CookieService,
    private translateService:TranslateService,
    //private overlayService: OverlayService
  ){
     // No provider for Overlay! trouble 
    //  const overlayRef = this.overlayService.overlay.create();
    //  //touch overlay
    //  console.log(overlayRef);

  }


  ngOnInit(): void {
    // it was same trouble for cookieService but it's gone
     const lang=this.cookieService.get('lang')||'en';
     //touch cookieService
     console.log(lang);

    this.translateService.setDefaultLang('en');
    this.translateService.use(lang);
    // NullInjectorError: No provider for TranslateStore! patch from https://gitmemory.com/issue/ngx-translate/core/883/532257699
    // does not work.
    this.translateService.store.onLangChange.subscribe((lang) => this.translateService.use(lang));

  }

}

我得到的错误是:

“错误:未捕获(承诺):NullInjectorError:StaticInjectorError(AppModule)[TranslateService-> TranslateStore]:在惰性模块中

“错误错误:未捕获(承诺):NullInjectorError:StaticInjectorError(AppModule)[InjectionToken cdk-connected-overlay-scroll-strategy-> Overlay]:StaticInjectorError(平台:核心)[InjectionToken cdk-connected-overlay-scroll-strategy->叠加]:NullInjectorError:没有用于覆盖的提供程序!“

对于“没有注释的叠加组件版本”。

这里是stackblitz

angular lazy-loading
1个回答
-1
投票

似乎您没有像这样在TranslateService app.module.ts数组中包含providers

providers : [TranslateService ]
© www.soinside.com 2019 - 2024. All rights reserved.