代码之家  ›  专栏  ›  技术社区  ›  Ricardo Saracino

使用普通旧对象的Angular 7.0u ID注入

  •  0
  • Ricardo Saracino  · 技术社区  · 7 年前

    我正在使用locale注入,并且我能够将它注入到构造函数中,我想知道是否可以将它作为属性注入。这将带我到我的下一个问题,我能注入一个简单的模型吗。我正在使用 class-transformer

    export class AppComponent implements OnInit {  
    
      @Inject(LOCALE_ID) public localeProperty: string;
    
      constructor(@Inject(LOCALE_ID) public locale: string, public appService: AppService) {
      }
    
      ngOnInit() {
    
        console.log('AppComponent.locale: ', this.locale);
        console.log('AppComponent.localeProperty: ', this.localeProperty);
    

    AppComponent.locale:法语

    AppComponent.localeProperty:未定义

    在我的位置模型中,我希望模型知道要使用什么标签,理想情况下,我希望能够注入LOCALE\u ID

    import {locale} from '../../environments/locale';
    
    
    export class Location {
    
      enLabel: string;
      frLabel: string;
    
      // can i do something here
      constructor() {
      }
    
      get label() {
        return this[`${locale}Label`] ? this[`${locale}Label`] : this.enLabel;
      }
    

    我是吉特 https://github.com/ricardosaracino/class-transformer

    0 回复  |  直到 7 年前
    推荐文章