使用响应中的后端信息:Angular

时间:2018-06-22 15:38:20

标签: arrays angular http angular-ui-router

我的后端给了我一系列文本:

['lamIt','lamItLite']

这是我获取lamIt或lamItLite版本的GET请求:

getVersion() {
    const url = this.lamItUrl + this.versionExt + 'lamit/version';
    console.log(this.environment.getConfig('Url'));
    const params = new HttpParams()
        .set('lanId', this.authService.username)
        .set('domain', 'NAM');
    const options = {
        params: params,
        responseType: 'text'as 'text' // Getting back text not JSON.
    };
    return this.http.get(url, options);
}

这是我重新路由用户的方式,以便在提交表单后输入lamIt或lamItLite:

onFormSubmit(f: NgForm) {
    if (this.signInForm.valid) {
      this.authService.username = this.signInForm.value.username;
      this.authService.password = this.signInForm.value.password;
      this.authService.loginUser().subscribe(
        response => {
          this.authService.isAuthenticated = true;
          this.authService.storeToken(response['access_token'], response['refresh_token']);
          this.daq.getVersion().subscribe(
            version =>  {
              this.authService.data = version;
              console.log(version);
              if (version === 'lamIt') { // checks if user has lam, it access, backend returned text not string, see daq service.
                this.router.navigate(['/lamit']);
              } else if (version === 'lamItLite') {
                this.router.navigate(['/lamitlite']);  // checks if user has lam it lite access, backend returned text not string, see lam service.
              } else if (version === 'lamIt' || 'lamItLite') {
                this.router.navigate(['/lamit']);  // checks if user has lam it and lamit lite, backend returned text not string, see lam service.
              }
              this.onLoginError(this.groupLoginErrorMessage);

            }
          );
        }, (error) => {
          console.log(error);
          this.onLoginError(this.invalidCredentialsMessage); // wrong password for auth
        }
      );
    }
  }

如何使用前端提供的后端内容(前面提到的文本数组)将用户重定向到正确的应用程序?

问题是,我不知道如何使用后端给我的东西,所以我可以将用户重定向到应用程序的正确版本。

1 个答案:

答案 0 :(得分:1)

由于后端返回数组,因此需要遍历数组

 this.daq.getVersion().subscribe(
     version =>  {
          this.authService.data = version;
          console.log(version);
          version.foreach((item) => {
               this.router.navigate(['/' + lamit]);
          }) 
          this.onLoginError(this.groupLoginErrorMessage);

     }
 );