简体   繁体   English

Angular 2,ngOnChanges,ngDoCheck

[英]Angular 2, ngOnChanges, ngDoCheck

Hello folks i have come to you with many thoughs, i would like to talk about something i cannot get. 大家好,我来过很多事,我想谈谈我无法获得的东西。

Simple service studies 简单服务研究

So we have here one service who is triggered by a component. 因此,这里有一个由组件触发的服务。

This is a simple service who commit an observable element when it get triggered : 这是一个简单的服务,它在触发时提交一个可观察的元素:

return this.subject.asObservable();

Then we have a component getting this : 然后我们有一个组件得到这个:

this.alertService.getMessage().subscribe(message => { this.message = message; });

Ok until now everything is ok. 好的,直到现在一切都很好。 This logic is include into a component , his selector name is <alert></alert> 此逻辑包含在组件中,其选择器名称为<alert></alert>

Nested Component 嵌套组件

Here we have another component with this <alert></alert> . 在这里,我们还有另一个<alert></alert>

<div class="article-page-container">
  <alert></alert>
  <ul *ngIf="!alert" class="page-nav-container">
    <li *ngFor="let page of pageArray" (click)="pageHandlerEvent(page)" [ngClass]="{ 'page-active': page == pageActive}">{{ page }}</li>
  </ul>
</div>

Did you get it ? 你明白了吗 ? What i am trying to do is when the nested component is getting an alert it should turn of the page nav class. 我想做的是,当嵌套的组件收到警报时,它应该打开页面导航类。

I have found one solution, this is not a problem about how to makes it works, but find the right one, made something that people will be proud of ? 我找到了一个解决方案,这不是如何使它起作用的问题,而是找到合适的解决方案,使人们为之骄傲的事情是什么?

ngDoCheck ngDoCheck

Do i need to say more ? 我需要说更多吗? It's ugly , i get like 1000000000 useless check, but it will works. 这很丑陋,我得到了10亿张无用的支票,但可以使用。

ngDoChanges ngDoChanges

Should only happens when something inside my component is actually getting 'change', right ? 仅当组件内部的某些东西实际上发生“变化”时才应该发生,对吗?

So i have try this : 所以我试试这个:

<div class="article-page-container">
  <alert (alertOn)="toggleNav($event)"></alert>
  <ul *ngIf="!alert" class="page-nav-container">
    <li *ngFor="let page of pageArray" (click)="pageHandlerEvent(page)" [ngClass]="{ 'page-active': page == pageActive}">{{ page }}</li>
  </ul>
</div>

Everytime an alert is triggered i do a simple this.alertOn.emit(true) , 每次触发警报时,我都会做一个简单的this.alertOn.emit(true)

However ngOnChanges doesn't work at all. 但是ngOnChanges根本不起作用。 And this will result with : 这将导致:

core.umd.js:3493 ORIGINAL EXCEPTION: Expression has changed after it was checked. Previous value: 'true'. Current value: 'false'.

Other tests 其他测试

  • I have try to include direclty this service into the container (same issue) 我已尝试将此服务包含在容器中(相同问题)

  • I have move the alert outside the component, using an Input with 我已经使用输入将警报移动到组件外部
    ngOnChanges work (another solution, but looks likes more i'm avoiding my problem more than finding a solution) ngOnChanges工作(另一个解决方案,但看起来更多的是我比找到解决方案更能避免遇到问题)

I know you guys are great, so if you have any idea about what should be done in a 'correct way'. 我知道你们很棒,所以如果您对应该以“正确的方式”做些什么有任何想法。

Have an awesome day ! 祝你有愉快的一天 !

Have you tried moving the 您是否尝试过移动

this.alertService.getMessage().subscribe(message => { this.message = message; });

subscription to your parent container, and then sending the message as an input through 订阅父容器,然后通过以下方式将消息作为输入发送

<alert [message]=message></alert>

this way you can also include some logic to change an display flag boolean for your page-nav-container in the subscribe function 这样,您还可以在subscribe函数中包含一些逻辑来更改您的page-nav-container的显示标志布尔值

So here the result i found that looks great ! 所以在这里我发现的结果看起来很棒!

@ViewChild(AlertComponent) AlertComponent: AlertComponent; 

.... 

ngAfterContentChecked () {
  this.alert = (this.AlertComponent.message) ? true : false;
}

Simple, it doesn't need to change the logic also it will not loose any perf compare to ngDocheck() with it's 10000k tests ... 简单,它不需要更改逻辑,也不会因为10000k测试而与ngDocheck()相比失去任何性能...

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

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