Typescript 未找到属性为的ng生成错误
我在Typescript中定义了any类型的对象,但是当我使用Typescript 未找到属性为的ng生成错误,typescript,angular2-services,Typescript,Angular2 Services,我在Typescript中定义了any类型的对象,但是当我使用ng build--prod--aot构建时,它显示属性不存在auth_user和auth_password export const environment = { production: false, api_url: 'http://localhost:8033', token_name: 'X-Auth-Token', auth_user: 'X-Auth-Username', auth_password:
ng build--prod--aot
构建时,它显示属性不存在auth_user
和auth_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))
}
}
@Injectable()
导出类UserService实现OnInit{
构造函数(专用apiService:apiService,专用jwtService:jwtService){
}
恩戈尼尼特(){
}
saveUser(用户:用户):可观察{
返回此.apiService.post(“/user”,user);
}
验证(用户:用户){
var有效负载:任意={
[environment.auth_password]:user.password,
[environment.auth\u user]:user.username
}
返回this.apiService.post(“/authenticate”,{},新头(有效负载))
}
}
以这种方式尝试对象的定义。交换“=”由“:”
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'
};
环境文件夹中有两个文件
environment.ts
和environment.prod.ts
我正在执行命令ngbuild--prod--aot
因此,它在构建时使用了environment.prod.ts
但是在environment.prod.ts中,这些属性没有定义,因为在将这些属性添加到environment.prod.ts之后,我从未为生产构建过
它工作正常,可以在开发环境中正常工作