我有一个Angular 5应用程序,我必须调用一些繁重的REST服务(通常需要几秒钟).我需要在应用程序的不同部分使用它的结果,所以我想将结果存储在DataStorageService中.基本上,这是我想实现的:
@Injectable()
export class DataStorageService {
private result: MyCustomObject;
constructor(private service: Service) {}
getResult(): MyCustomObject {
if (typeof this.result === 'undefined') {
// save result
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
问题是我如何等待HTTP请求完成,然后保存并返回'result'对象.我试图用Promise和Observable来解决它,但是没有一个能正常工作.
观察到:
if (typeof this.result === 'undefined') {
this.service.call()
.subscribe(response => this.result = response);
}
return this.result; // wait for save and return MyCustomObject
Run Code Online (Sandbox Code Playgroud)诺言:
if (typeof this.result === 'undefined') {
this.service.call()
.toPromise()
.then(response => this.result = response);
}
return this.result; // wait for save and return MyCustomObject
Run Code Online (Sandbox Code Playgroud)我有一个 Angular 应用程序,我定期更新并使用最新的 Webpack 打包器构建它。
几天前,我将项目升级到 Angular 9,遇到了一些我无法解决的问题。
plugins: [
new ngw.AngularCompilerPlugin({
tsConfigPath: path.resolve(rootPath, 'tsconfig.aot.json'),
entryModule: path.resolve(rootPath, 'src', 'app', 'app.module#AppModule')
})
]
Run Code Online (Sandbox Code Playgroud)
你可以在下面找到我的配置文件,但整个项目都可以在 GitHub 上找到:https : //github.com/aszidien/angular-webpack-build
package.json
...
"scripts": {
"build:dev": "cross-env NODE_ENV=development webpack --mode development",
"build:prod": "cross-env NODE_ENV=production webpack --mode production",
"test": "jest"
}
...
Run Code Online (Sandbox Code Playgroud)
tsconfig.aot.json
{
"compilerOptions": {
"module": "es2015",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true, …Run Code Online (Sandbox Code Playgroud)