angular - 翻译在错误组件 angular2/6 中不起作用
问题描述
在加载应用程序时,我正在调用将加载主数据的服务。如果由于任何网络问题未加载主数据。然后我通过将错误存储在本地存储中并检索错误组件中的错误来导航到错误组件。在 Error.component.html 我使用 "| translate" 。这是行不通的。所有其他 html 文件都在使用翻译管道。
应用模块
export function createTranslateLoader(http: Http) {
return new TranslateStaticLoader(http, './assets/i18n', '.json');
}
@NgModule({
declarations: [
AppComponent
],
imports: [
// :CORE MODULE: //
BrowserModule,
BrowserAnimationsModule,
HttpClientModule,
TranslateModule.forRoot(
{
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [Http]
}),
FormsModule,
CommonModule, //<====added
//:3RD PARTY MODULE://
BootstrapModalModule,
//:APPLICTION MODULES: //
AppLoadModule, //Startupdata before APP loaded
AppRoutingModule,
FooterModule,
ErrorModule,
AccessDeniedModule,
NotFoundModule,
RouterModule.forRoot([]),
ToasterModule.forChild(),
],
providers: [
LoaderService,
ToasterService,
StartupService,
ResponseStatusesService,
LocalStorageService,
ApplicationSettingsService,
LabSelectionService,
AccountService,
AuthService,
AlertService,
AuthGuard,
RolesGuard,
FeaturebasedGuard,
ErrorLogService,
{
provide: ErrorHandler,
useClass: GlobalErrorsHandler
},
{
provide: HTTP_INTERCEPTORS,
useClass: AppHttpInterceptor,
multi: true
},
{
provide: LocationStrategy,
useClass: HashLocationStrategy
},
],
exports: [],
bootstrap: [AppComponent]
})
export class AppModule { }
应用加载模块
export function load(appLoadService: AppLoadService) {
return () => appLoadService.load();
}
@NgModule({
imports: [HttpClientModule],
providers: [
AppLoadService,
{ provide: APP_INITIALIZER, useFactory: load, deps: [AppLoadService], multi: true }
]
})
export class AppLoadModule { }
应用加载服务
load(): Promise<any> {
return new Promise(resolve => {
const promise = this.httpClient.get('assets/settings/config.json')
.subscribe(
res_config => {
/*=========== AUTH CALL API - START ===========*/
this.httpClient.get('/api/auth')
.subscribe(
res_auth_context => {
this.storeUserContext(res);
console.log('@auth context loaded step3');
/*=========== LOAD master data ===========*/
this.httpClient.get('/xyz/masterData')
.subscribe(
res => {
console.log(" master data Loaded");
resolve();
},
err => {
debugger;
this.localStorage.store("error", err);
let router = this.injector.get(Router);
router.navigate(['error']);
resolve();
}
);
/*=========== LOAD Mastre data ===========*/
},
res_auth_context_error => {
console.log("Auth Call Failed");
}
);
/*=========== AUTH CALL API - END ===========*/
},
res_config_Error => {
console.log("config file NOT loaded", res_config_Error);
}
);
});
}
应用路由模块
const routes: Routes = [
{
path: '',
children: [
{ path: '', loadChildren: './home/home.module#HomeModule' },
{ path: 'error', loadChildren: './core/error/error.module#ErrorModule' },
{ path: 'accessDenied', loadChildren: './accessDenied/access-denied.module#AccessDeniedModule' },
{ path: 'notfound', loadChildren: './notFound/not-found.module#NotFoundModule' },
{ path: '**', redirectTo: '/notfound' }
]
}
];
@NgModule({
declarations: [],
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
错误路由模块
const routes: Routes = [
{
path: '', component: ErrorComponent
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class ErrorRoutingModule { }
error.component.html
<p class="m-y text-muted h4">
<a (click)="logout()" class="text-danger">{{'logout'|translate}} and Re-login</a>
</p>
<textarea [(ngModel)]='errorMessage' [disabled]="true" rows="10" cols="120"></textarea>
错误模块
@NgModule({
imports: [
CommonModule,
FormsModule,
ErrorRoutingModule,
TranslateModule
],
declarations: [ErrorComponent]
})
export class ErrorModule { }
解决方案
推荐阅读
- python - 打印 n 对括号的所有有效组合的算法
- c++ - 我应该怎么做才能优化这个 C++ 代码?我不知道哪个语句需要更多的执行时间
- swift - 滚动视图分页内容大小不适用于 iphone 5s ios 12
- r - 消除列 R 中的字符
- python - 如何让我的 JSON 解码器正常工作?
- powershell - 在 PowerShell 中结合 `Get-Disk` 信息和 `LogicalDisk` 信息(但带有格式化输出)
- reactjs - react-top-loading-bar 警告:无法对未安装的组件执行 React 状态更新
- swift - SwiftUI - init 函数中的 .append 不起作用
- python - 尝试使用 opencv 在 tkinter 窗口上显示视频时出现内存错误
- docker - 无法从 Docker 容器连接到外部 oracle 数据库