给定以下组件
@Component({
selector: 'sidebar-menu',
templateUrl: './sidebar-menu.component.html',
styleUrls: ['./sidebar-menu.component.scss'],
})
export class SidebarMenuComponent implements OnInit {
userMenuItems: MenuItem[] = [];
constructor(private readonly service: TranslateService) {}
ngOnInit() {
this.userMenuItems = [
{
label: this.service.instant('menu.sidebar.user.global-user-management'),
routerLink: 'admin-users',
},
{
label: this.service.instant('menu.sidebar.user.centre-user-management'),
routerLink: '/',
},
];
}
}
我正在尝试获取要翻译的菜单项的标签。但上面的方法失败了。应用此翻译的正确方法是什么?
提前致谢
此问题的答案已在本出版物中得到充分解答。总之。 instant 是同步的,翻译不可用。解决了
.get
ngOnInit() {
this.service
.get([
'menu.sidebar.user.global-user-management',
'menu.sidebar.user.centre-user-management',
])
.subscribe({
next: () => {
this.userMenuItems = [
{
label: this.service.instant(
'menu.sidebar.user.global-user-management'
),
routerLink: 'admin-users',
},
{
label: this.service.instant(
'menu.sidebar.user.centre-user-management'
),
routerLink: '/',
},
];
},
});
}
一种选择是使用
stream
globalusermanagement: string = "";
ngOnInit() {
this.service.stream('global-user-management').subscribe((res: string) => {
this.globalusermanagement = res;
});
this.userMenuItems = [
{
label: this.service.stream('menu.sidebar.user.global-user-management'),
routerLink: 'admin-users',
}
];