小编asz*_*ien的帖子

Angular 5同步HTTP调用

我有一个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来解决它,但是没有一个能正常工作.

  1. 观察到:

    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)
  2. 诺言:

    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)

http promise observable angular

12
推荐指数
1
解决办法
2万
查看次数

使用 webpack 构建 Angular 9

我有一个 Angular 应用程序,我定期更新并使用最新的 Webpack 打包器构建它。
几天前,我将项目升级到 Angular 9,遇到了一些我无法解决的问题。

  1. npm run build:prod
    错误 TS2307:找不到模块“./app/app.module.ngfactory”。
    我认为这是由 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)
  1. npm run test
    File not found: ./node_modules/jest-preset-angular/InlineHtmlStripStylesTransformer(解析为:.../angular-webpack-build/node_modules/jest-preset-angular/InlineHtmlStripStylesTransformer)
    这个错误对我来说有点神秘. 我读过一些关于类似问题的帖子,但这些建议没有奏效。

你可以在下面找到我的配置文件,但整个项目都可以在 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)

webpack angular

6
推荐指数
1
解决办法
7071
查看次数

标签 统计

angular ×2

http ×1

observable ×1

promise ×1

webpack ×1