我有一个angular 9应用程序,该应用程序具有用于浏览器和服务器的不同模块,所有功能在不久前都可以运行,但是现在我注意到我的服务器模块的功能根本无法启动。
app.server.module.ts:
import { NgModule } from '@angular/core';
import { ServerModule, ServerTransferStateModule } from '@angular/platform-server';
import { BrowserModule, TransferState } from '@angular/platform-browser';
import { AppModule } from './app.module';
import { AppComponent } from './app.component';
import { TranslateServerLoader } from './translate-server-loader.service';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
export function translateFactory(transferState: TransferState) {
return new TranslateServerLoader('/assets/i18n','.json', transferState);
}
@NgModule({
imports: [
AppModule,
ServerModule,
ServerTransferStateModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: translateFactory,
deps: [TransferState]
}
}),
BrowserModule.withServerTransition({ appId: 'test' }),
],
bootstrap: [AppComponent],
})
export class AppServerModule { }
和TranslateServerLoader
的功能:
import { Observable } from "rxjs";
import { TranslateLoader } from '@ngx-translate/core';
declare var require: any;
import { join } from 'path';
import { makeStateKey, StateKey, TransferState } from '@angular/platform-browser';
const fs = require('fs');
export class TranslateServerLoader implements TranslateLoader {
constructor(private prefix: string = 'i18n',
private suffix: string = '.json',
private transferState: TransferState) {
}
public getTranslation(lang: string): Observable<any> {
console.log('translating in server....');
return Observable.create(observer => {
const assets_folder = join(process.cwd(), 'dist', 'autorent', 'browser', this.prefix);
console.log('TEST PATH - ', assets_folder);
const jsonData = JSON.parse(fs.readFileSync(`${assets_folder}/${lang}${this.suffix}?v2`, 'utf8'));
// Here we save the translations in the transfer-state
const key: StateKey<number> = makeStateKey<number>('transfer-translate-' + lang);
this.transferState.set(key, jsonData);
observer.next(jsonData);
observer.complete();
});
}
}
因此从未调用过称为 getTranslation()的函数,因为在运行服务器时看不到任何控制台日志,为什么?