代码之家  ›  专栏  ›  技术社区  ›  petko_stankoski

找不到管道“translation”

  •  0
  • petko_stankoski  · 技术社区  · 4 年前

    <div class="my-container">
      <span>{{ 'locations' | translation }}</span>
    </div>
    

    这是翻译.pipe.ts:

    import { Pipe, PipeTransform } from '@angular/core';
    import { TranslationsService } from './translations.service';
    
    @Pipe({ name: 'translation' })
    export class TranslationPipe implements PipeTransform {
      constructor(private myTranslationsService: TranslationsService) {}
    
      transform(value: string): string {
        return this.myTranslationsService.get(value);
      }
    }
    

    这就是测试模块.ts文件:

    import { TestComponent } from './test.component';
    import { Injector, DoBootstrap, NgModule } from '@angular/core';
    import { createCustomElement } from '@angular/elements';
    import { BrowserModule } from '@angular/platform-browser';
    import { HttpClientModule } from '@angular/common/http';
    import { FormsModule } from '@angular/forms';
    import { TranslationsService } from '../shared/translations.service';
    import { TranslationPipe } from '../shared/translation.pipe';
    
    @NgModule({
      declarations: [TestComponent, TranslationPipe],
      imports: [BrowserModule, FormsModule, HttpClientModule],
      entryComponents: [TestComponent],
      providers: [TranslationsService],
      exports: []
    })
    export class TestModule implements DoBootstrap {
      constructor(private injector: Injector) {
    
      }
    
      ngDoBootstrap() {
        const ngElement = createCustomElement(TestComponent, { injector: this.injector, });
        customElements.get('my-test') || customElements.define('my-test', ngElement);
      }
    }
    

    当我运行的应用程序,我得到控制台错误,管道'翻译'找不到!我做错什么了?

    2 回复  |  直到 4 年前
        1
  •  1
  •   Gauri Kesava Kumar    4 年前

    尝试导入CommonModule

    import {CommonModule} from "@angular/common";
    
        2
  •  -1
  •   Antoniossss    4 年前

    添加你的 TranslationPipe providers

      providers: [TranslationsService, TranslationPipe],