gpt4 book ai didi

Angular ngx-translate Karma 测试失败,TypeError : this. missingTranslationHandler.handle is not a function

转载 作者:行者123 更新时间:2023-12-05 08:05:52 24 4
gpt4 key购买 nike

按照本教程 (https://indepth.dev/implementing-multi-language-angular-applications-rendered-on-server/),我已经建立了一个运行良好的简单语言翻译系统。但是当我启动我的测试时,我遇到了很多错误,然后我将我的 Angular 从 8 升级到 10,解决了一些错误,但是这个仍然是 remaning,我现在不知道如何修复它:TypeError: this. missingTranslationHandler.handle 不是函数

有时我也有一个 this.currentLoader.getTranslation is not a function 错误现在不知道为什么。

包.json

"dependencies": {
"@angular/animations": "~10.0.7",
"@angular/common": "~10.0.7",
"@angular/compiler": "~10.0.7",
"@angular/core": "~10.0.7",
"@angular/forms": "~10.0.7",
"@angular/platform-browser": "~10.0.7",
"@angular/platform-browser-dynamic": "~10.0.7",
"@angular/router": "~10.0.7",
"@ngx-translate/core": "^13.0.0",
"@ngx-translate/http-loader": "^6.0.0",
"rxjs": "~6.6.2",
"tslib": "^2.0.0",
"zone.js": "~0.10.3"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.1000.5",
"@angular/cli": "~10.0.5",
"@angular/compiler-cli": "~10.0.7",
"@angular/language-service": "~10.0.7",
"@types/node": "^12.11.1",
"@types/jasmine": "~3.3.8",
"@types/jasminewd2": "~2.0.3",
"codelyzer": "^6.0.0",
"jasmine-core": "~3.5.0",
"jasmine-spec-reporter": "~5.0.0",
"karma": "~5.0.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage-istanbul-reporter": "~3.0.2",
"karma-jasmine": "~3.3.0",
"karma-jasmine-html-reporter": "^1.5.0",
"protractor": "~7.0.0",
"ts-node": "~7.0.0",
"tslint": "~6.1.0",
"typescript": "~3.9.7"
}

i18n/i18n.module.ts

@NgModule({
imports: [
HttpClientModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: translateLoaderFactory,
deps: [HttpClient]
}
}),
],
exports: [TranslateModule]
})
export class I18nModule {
constructor(translate: TranslateService) {
translate.addLangs(['en', 'fr']);
const browserLang = translate.getBrowserLang();
translate.use(browserLang.match(/en|fr/) ? browserLang : 'en');
}
}

export function translateLoaderFactory(httpClient: HttpClient) {
return new TranslateHttpLoader(httpClient);
}

应用程序模块.ts

@NgModule({
declarations: [
AppComponent,
NavigationComponent,
HomeComponent
],
imports: [
BrowserModule,
AppRoutingModule,
I18nModule,
TranslateModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

home/home.componenet.ts

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

public currentLang: string;
text = '';

constructor(private changeDetectorRef: ChangeDetectorRef, public translate: TranslateService) { }

ngOnInit() {
this.currentLang = this.translate.currentLang;
this.translate.onLangChange.subscribe((event: LangChangeEvent) => {
this.currentLang = event.lang;
this.changeDetectorRef.detectChanges();
this.translateText();
});
console.log('text: ', this.text);
}

translateText() {
this.translate.get('default').subscribe((data: any) => {
this.text = data.compA;
console.log('text: ', this.text);
});
}

}

home/home.componenet.html

<select #langSelect (change)="translate.use(langSelect.value)">
<option *ngFor="let lang of translate.getLangs()" [value]="lang"
[attr.selected]="lang === translate.currentLang ? '' : null">{{lang}}</option>
</select>

<p>{{'default.compA' | translate}}</p>

<p translate>default.compB</p>

<p>{{text}}</p>

home/home.component.spec.ts

describe('HomeComponent', () => {
let component: HomeComponent;
let fixture: ComponentFixture<HomeComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [HomeComponent],
imports: [
TranslateModule
],
providers: [
TranslateService,
TranslateStore,
TranslateLoader,
TranslateCompiler,
TranslateParser,
MissingTranslationHandler,
{ provide: USE_DEFAULT_LANG, useValue: undefined },
{ provide: USE_STORE, useValue: undefined },
{ provide: USE_EXTEND, useValue: undefined },
{ provide: DEFAULT_LANGUAGE, useValue: undefined }
]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(HomeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});

assets/i18n/en.json

{
"default": {
"compA": "Component A works",
"compB": "Component B works"
}
}

assets/i18n/fr.json

{
"default": {
"compA": "Le Composant A fonctionne",
"compB": "Le Composant B fonctionne"
}
}

最佳答案

以防将来有人遇到这个问题:

imports: [
TranslateModule.forRoot(),
],
providers: [
TranslateService
]

您需要在 *.spec.ts 中为您的模块使用 forRoot() 方法。您可以在此处找到有关 forRoot 的更深入的解释:https://medium.com/slackernoon/when-to-use-angulars-forroot-method-400094a0ebb7

关于Angular ngx-translate Karma 测试失败,TypeError : this. missingTranslationHandler.handle is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63250986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com