首页 > 解决方案 > 如何在 Angular 应用程序开始时设置 ABP 显示语言?

问题描述

我正在开发一个作为 MVC UI 项目开始的项目。但是,我们决定将 Angular 用于应用程序的一小部分。我能够让它与不同的 ABP 服务一起工作;但是,我目前坚持尝试在 Angular 应用程序启动时设置本地化服务使用的语言,以便它与用户在应用程序的 MVC 部分中配置的语言相同。

我们使用的是 ABP 3.0.5 版。

我的想法是使用 Web 服务获取用户在 ASP.NET 端选择的语言,然后使用SetLanguage. 使用默认 Angular 模板中的语言选择代码,我想出了这样的东西:

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  title = 'MyAngularApp';

  constructor(
    private userInfo: UserInfoService,
    private store: Store) { }

  ngOnInit(): void {
    this.userInfo.getLanguage().subscribe(result => {
      this.store.dispatch(new SetLanguage(result));
    });
  }
}

在这段代码中,该getLanguage函数工作并返回正确的语言代码(en例如)。但是,似乎调用this.store.dispatch(new SetLanguage(result))与角度路由器混淆并将我的路由重置为其当前值。例如,如果我想导航到https://localhost:12345/angularApp/?val=00112233更改语言会导致应用程序被重定向到https://localhost:12345/angularApp/.

我是否使用了正确的方法来初始化应用程序语言?你会为这样的任务推荐什么?

标签: angularabp

解决方案


推荐阅读