在Angular中进行插值以防止从操作调用Reducer。

huangapple go评论60阅读模式
英文:

Interpolation in Angular preventing Reducer to be called from Action

问题

我正在开发一个使用ngrx进行状态管理的Angular应用程序。

我将一个子组件引入到父组件中,并注意到现有的ExistingAction的reducer没有被调用。

ParentComponent.html

...
<childComponent> </childComponent>
...

ParentComponent.ts

export class ParentComponent {

 ....

  this.store.dispatch(new ExistingAction());

...

}

ChildComponent.ts

export class ChildComponent {

 displayString: any;

 subscription$: Subscription;

 ....

 ngOnInit() {

   this.subscription$ = this.service.subscribe((content) => {

                                  this.displayString = content;

  }
 ngOnDestroy()
 {
   this.subscription.unsubscribe();
 }

 ....

}

ChildComponent.html

<label class="display">
  Sample : {{displayString['SelectSample']}}
</label>

服务的输出结果是JSON,如下:'SelectSample' : 'This is text'

尽管ChildComponent.html中的上述代码在屏幕上打印文本,并且控制台中没有错误,但我发现由于引入ChildComponent,父组件中Action的Reducer函数停止被调用。

经过一系列的试验和错误,我在ChildComponent中进行了以下更改,并出奇不意地Reducer再次被调用,流程正常工作。

这是我所做的更改。

a)

<label class="display">
  Sample : {{displayString}}
</label>

b)

this.subscription$ = this.service.subscribe((content) => {
                                  
                                  this.displayString = content['SelectSample'];
}

所以我的问题是,为什么像这样的插值{{displayString['SelectSample']}}使用会阻止Reducer被调用?插值和ngrx的Reducers之间有什么联系?我无法在任何地方找到答案。

WorkingCode

ChildComponent.ts

export class ChildComponent

{

 displayString: any;

 subscription$: Subscription;

 ....

 ngOnInit()

 {

   this.subscription$ = this.service.subscribe((content) => {

                                  this.displayString = content['SelectSample'];

  }
 ngOnDestroy()
 {
   this.subscription.unsubscribe();
 }

 ....

}

ChildComponent.html

<label class="display">
  Sample : {{displayString}}
</label>
英文:

I am working on an Angular app which uses ngrx for State management .

I introduced a child component to the parent and noticed that reducer from ExistingAction was not getting invoked (ExistingAction) .

ParentComponent.html

...

&lt;childComponent&gt; &lt;/childComponent&gt;
...

ParentComponent.ts

export class ParentComponent 

{

 ....

  this.store.dispatch(new ExistingAction());

...

}

ChildComponent.ts

    export class ChildComponent
    
    {
    
     displayString : any ;
    
    subscription$ : subscription;
    
    .... 
    
     ngOninit()
    
     {
    
       this.subscription$ = this.service.subscribe((content) =&gt; {
    
                                      this.displayString= content;
    
      }
    }
   ngOnDestroy()
   {
     this.subscription.unsubscribe();
   }
    
    ....
    
  }

ChildComponent.html

&lt;label class=&quot;display&quot;&gt;

  Sample : {{displayString[&#39;SelectSample&#39;]}}

&lt;/label&gt;

Output from the service is json like 'SelectSample' : 'This is text'

Although the above code in ChildComponent.html prints text on the screen and no errors found in console , I found that the Reducer fn from the Action in ParentComponent was stopped being called because of introduction of ChildComponent.

After a series of trial and error I made the following change in ChildComponent and surprisingly the Reducer was invoked again and the flow worked normally .
Here is what I changed.

a) 

&lt;label class=&quot;display&quot;&gt;

          Sample : {{displayString}}

      &lt;/label&gt;

-> html changes

b) 

this.subscription$ = this.service.subscribe((content) =&gt; {
        
                                          this.displayString= content[SelectSample];
    } 

-> .ts changes

So my question why is usage of interpolation like this {{displayString['SelectSample']}} , preventing the Reducer from being called ? whats the connection between interpolation and ngrx Reducers? I could not find answer anywhere for this

WorkingCode

ChildComponent.ts

export class ChildComponent

{

 displayString : any ;

subscription$ : subscription;

.... 

ngOninit()

 {

   this.subscription$ = this.service.subscribe((content) =&gt; {

                                  this.displayString= content[SelectSample];

  }
 ngOnDestroy()
 {
   this.subscription.unsubscribe();
 }

....

}

ChildComponent.html

&lt;label class=&quot;display&quot;&gt;

  Sample : {{displayString}}

&lt;/label&gt;

答案1

得分: 1

I'm here to provide Chinese translations. Here's the translation of the text you provided:

这个问题很难找到根本原因,如果没有看到所有的代码(最好是一个复现)。

但是回答你的问题,插值不会影响 "NgRx 流程"。
它可能会引发错误,导致动作未分发/视图未刷新。

英文:

It's a hard to find the root cause of this without seeing all of the code (even better is a reproduction).

But to answer the question the interpolation does not impact the "NgRx flow".
It could perhaps throw an error, which results that the action is not dispatched/view is not refreshed.

huangapple
  • 本文由 发表于 2023年7月31日 22:05:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定