typescript - 我在 Angular 17 的 environment.ts 文件中使用属性 'endpoint' 时收到错误
我正在尝试在我的服务中使用 environment.ts 中的属性 endpoint:
export const environment = {
production: false,
endpoint: 'http://localhost:3000/api/cabin/'
};
这是我的服务:
import {Injectable} from '@angular/core';
import {environment} from "../../environments/environment";
import {HttpClient} from "@angular/common/http";
import {Cabin} from "../interfaces/cabin";
import {Observable} from "rxjs";
@Injectable({
providedIn: 'root'
})
export class CabinService {`your text`
private myAppUrl: string;
constructor(private http: HttpClient) {
this.myAppUrl = environment.endpoint;
}
getListCabins(): Observable<Cabin[]> {
return this.http.get<Cabin[]>(this.myAppUrl + '/getCabins');
}
}
但是当我使用该服务时,我收到这个错误:
[ERROR] TS2339: Property 'endpoint' does not exist on type '{}'. [plugin angular-compiler]
src/app/services/cabin.service.ts:14:32:
14 │ this.myAppUrl = environment.endpoint;
我能做什么?
答案#1
好的,在我的项目中,我有两个环境文件:environment.ts 和 environment.development.ts。我通过在 environment.development.ts 文件中添加相同的 endpoint 属性解决了错误,像这样:
export const environment = {
production: false,
endpoint: 'http://localhost:3000/api/cabin/'
};
但我不明白为什么这是解决方案,因为在我的服务中我导入了 environment.ts 文件。有人能帮我解答这个疑问吗?
