简体   繁体   English

Angular 在 promise 内发射不工作

[英]Angular emit inside a promise not working

In my Angular project I want to emit inside the.then and.catch, but they do net get called.在我的 Angular 项目中,我想在.then 和.catch 中发射,但它们确实被调用了。 Everything else inside the.then and.catch does get called. .then 和.catch 中的所有其他内容都会被调用。

When I emit outside the promise it does work.当我在 promise 之外发射时,它确实有效。

This is my code这是我的代码

updateAssignee(newAssignee: string) {
    this.storyService.updateAssignee(this.story, newAssignee).then(res =>{
      console.log('test')     //This one gets called
      this.onError.emit({     //This one does not get called
        error: false,
        errorText: ''
      });
    }).catch((error) => {
      console.log('test')     //This one gets called
      this.onError.emit({     //This one does not get called
        error: true,
        errorText: '• ' + error.message
      });
    });

    this.onError.emit({     //This one gets called
      error: false,
      errorText: '• Missing permissions'
    });
  }

Is it not possible to emit inside the.then and.catch?是不是不能在.then和.catch里面发射? Or am I missing something?还是我错过了什么?

The way these components are loaded is via a ngFor loop.这些组件的加载方式是通过 ngFor 循环。 Maybe this causes some problems?也许这会导致一些问题?

<span *ngFor="let story of done">
    <app-storyboard-story (onError)="throwError($event)" [story]="story" [project]="project" cdkDrag></app-storyboard-story>
</span>

UPDATED更新

Based on the code sample looks like ngFor detects changes and unsubscribes from the children components.根据代码示例, ngFor可以检测到子组件的更改并取消订阅。

The solution would be to implement trackBy https://angular.io/api/common/NgForOf , that should help to keep instances of children components when the done has been changed.解决方案是实现trackBy https://angular.io/api/common/NgForOf ,这应该有助于在更改done后保留子组件的实例。

ORIGINAL原来的

In the above above nothing is broken.在上面没有什么是坏的。 You can verify that it works via adding您可以通过添加来验证它是否有效

this.onError.subscribe(console.log);

before this.storyService call.this.storyService调用之前。

I would suggest you to investigate how the listener subscribes and unsubscribes and change its flow because looks like it unsubscribes (call ngIf or something that removes the component from the render) too early.我建议您调查侦听器如何订阅和取消订阅并更改其流程,因为看起来它取消订阅(调用 ngIf 或其他从渲染中删除组件的东西)太早了。

With the provided code it's impossible to give you better advice.使用提供的代码不可能给你更好的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM