通常我们把后端API的主机放在环境文件的一个条目中。。因此,您可以轻松地更改前端本地运行的不同阶段(test/qa/stanging/prod)。。无需重构服务:
类似于:
export const environment = {
production: false,
API_URL: 'http://localhost:1048/api/'
};
然后例如environment.staging.ts
export const environment = {
production: true,
API_URL: 'http://staging.mysite.com/api/'
};
然后在你的服务中:
//导入您的环境
import { environment } from '../../../environments/environment';
getUsersDetail(userName, userPassword){
return this.http.post(environment.APIURL + '/api/auth.php', {
userName,
userPassword
}, {responseType: 'text'}).pipe(
map(data => {
console.log(JSON.stringify(data));
}),
catchError(error =>{
return throwError("Something webt wrong"+JSON.stringify(error));
})
).subscribe( data => {
console.log(data, 'is what we got from server');
});
}