找不到属性的构建错误

时间:2018-04-15 15:35:28

标签: typescript angular2-services

我在Typescript中定义了一个类型为any的对象,但是当我使用ng build --prod --aot进行构建时,它显示该属性不存在auth_userauth_password

export const environment = {
  production: false,
  api_url: 'http://localhost:8033',
  token_name: 'X-Auth-Token',
  auth_user: 'X-Auth-Username',
  auth_password: 'X-Auth-Password'
};

authenticate(user: User) {
    var payload:{[x: string]: any} = {}
    payload[environment.auth_user] = user.username;
    payload[environment.auth_password] = user.password;
    return this.apiService.post("/authenticate", {}, new Headers(payload))
}

我也试过

var payload:any = {
      [environment.auth_password]:user.password,
      [environment.auth_user] : user.username
    }

但没有运气。 任何解决方案将不胜感激。

错误明细

ERROR in /home/vikram/code/angular/start/src/app/user/services/user.service.ts (26,20): Property 'auth_password' does not exist on type '{ production: boolean; api_url: string; }'.
ERROR in /home/vikram/code/angular/start/src/app/user/services/user.service.ts (27,20): Property 'auth_user' does not exist on type '{ production: boolean; api_url: string; }'.

这是我的完整服务

@Injectable()
export class UserService implements OnInit {


  constructor(private apiService: ApiService, private jwtService: JwtService) {
  }

  ngOnInit() {

  }
  saveUser(user: User): Observable<any> {
    return this.apiService.post("/user", user);
  }
  authenticate(user: User) {
    var payload:any = {
      [environment.auth_password]:user.password,
      [environment.auth_user] : user.username
    }
    return this.apiService.post("/authenticate", {}, new Headers(payload))
  }
}

2 个答案:

答案 0 :(得分:0)

以这种方式尝试对象的定义。交流&#39; =&#39;通过&#39;:&#39;

export const environment: {
  production: false,
  api_url: 'http://localhost:8033',
  token_name: 'X-Auth-Token',
  auth_user: 'X-Auth-Username',
  auth_password: 'X-Auth-Password'
};

答案 1 :(得分:0)

我在环境文件夹中有两个文件 environment.tsenvironment.prod.ts

我正在执行命令ng build --prod --aot

因此在构建

时需要environment.prod.ts

但是在environment.prod.ts中没有定义这些属性,因为在将这些属性添加到environment.prod.ts后我从未构建生产 它正在寻找