我们正在使用默认的i18n进行国际化。为此,我们具有用于多种语言的json资产文件,以及一个用于读取标签并将其显示在页面变量中的服务。因此我们在路由级别创建了一个解析器,所有页面都属于该解析器。一切工作正常,今天我要担心的是,如果语言环境发生变化,我们将从进行API调用的资产中获取标签。由于在解析器中我们正在进行api调用,将来,当我们在json文件中拥有更多名称时,它将在呈现页面时造成问题吗?还是有更好的方法来使语言环境可用于子组件?>
路线:
{
path: '',
component: CustomLayoutComponent,
resolve: { message: LangService },
children: [
{
path: '',
redirectTo: 'dashboard',
pathMatch: 'full'
},
{
path: dashboard
loadChildren: () =>
import(
'./modules/dashboard/dashboard.module'
).then(m => m.DashboardModule)
}
}
语言服务:
public resolve(route: any) {
const locale: string = route.queryParams.localeId
this.storeLocaleId.next(locale);
this.localeId = locale;
return this.getLabels(this.localeId);
}
getLabels(localeId: string) {
return this.http.get(this.labelURL).pipe(
map((data: any) => {
return data.labels;
})
);
}