Issue
I'm trying to use in my service the property endpoint from my environment.ts:
export const environment = {
production: false,
endpoint: 'http://localhost:3000/api/cabin/'
};
This is my service:
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');
}
}
But when I use the service I'm getting this error:
[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;
What can I do?
Solution
Okay, in my project I have two environment files: environment.ts and environment.development.ts. I solved the error by adding the same endpoint property to the environment.development.ts file, like this:
export const environment = {
production: false,
endpoint: 'http://localhost:3000/api/cabin/'
};
But I don't understand why this is the solution, because in my service I imported the environment.ts file. Can someone clarify this doubt for me, please?
Answered By - Vic
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.