angular - 具有 REST 服务的 Angular 资源:Web 应用程序和电子
问题描述
我们考虑将我们现有的 Angular Web 应用程序打包到一个电子项目中,以便离线运行。到目前为止,Web 应用程序通过 REST 请求加载配置。在电子应用程序中,此信息应位于本地文件中。
有什么建议可以有效地做到这一点吗?
我的第一个想法,在电子的快速服务器中模拟后端似乎并不常见。但是以角度重定向所有 REST 请求对我来说似乎也不是很直接。
解决方案
所以我要做的是创建一个 Http 扩展类,并根据您通过 http 请求发送的参数更新端点。
Http扩展器:
export function HttpAuthCreator(http: HttpClient) {
return new HttpAuth(http)
}
@Injectable()
export class HttpAuth {
// Extending the HttpClient through the Angular DI.
public constructor(public http: HttpClient) {
// If you don't want to use the extended versions in some cases you can access
the public property and use the original one.
// for ex. this.httpClient.http.get(...)
}
/**
* GET request
* @param string endPoint it doesn't need / in front of the end point
* @param IRequestOptions options options of the request like headers, body, etc.
* @returns Observable<T>
*/
public get<T>(endPoint: string, destination?: string, options?: IRequestOptions): Observable<T> {
return this.http.get<T>(
this.updateEndpoint(endPoint, destination),
this.configRequestOptions(options, destination)
)
}
然后你的子程序更新端点
private updateEndpoint(endpoint: string, destination?: string) {
if (destination === 'local') {
return `${environment.localAPI}${endpoint}`
} else {
return `${environment.onlineAPI}${endpoint}`
}
}
这样您就可以动态处理端点位置
get() {
return this.http.get(`/organizations/${params.id}`, 'local').toPromise()
}
推荐阅读
- progressive-web-apps - 为什么基于 Workbox 的 PWA 在 iOS 12 上从主屏幕启动时会产生错误?
- python - 为什么 pyqtSignal() 是一个类属性,而它实际上是一个实例属性?
- authentication - “无效的身份验证数据。连接重置”尝试在 IntelliJ 中登录 github 时
- apache - 从子目录中的 URL 中删除 .php
- android - 将 RadioButtons 动态添加到 RadioGroup 会中断互斥选择
- swift - 使用核心图像内核语言将颜色转换为 RGB 到 HSL
- python - 在python-OpenCV中确定相机和人/脸之间的距离
- android - 无法使用 Glide 将本地磁盘映像加载到 home 小部件远程视图中
- ruby-on-rails - 如何匹配在rails中以双下划线开头和结尾的字符串中的所有单词
- mysql - 如何在 Django 中扩展数据库和性能?