简体   繁体   English

Angular (9) 组件间共用state

[英]Angular (9) sharing common state between components

In current (2020) Angular, I have two components that are intended to share the state of activeProject through a service.在当前 (2020) Angular 中,我有两个组件旨在通过服务共享 activeProject 的activeProject I have the following defined on an ApplicationProjectService :我在ApplicationProjectService上定义了以下内容:

private activeProjectSource = new BehaviorSubject(undefined);
activeProject$ = this.activeProjectSource.asObservable();

set activeProject(v: any) {
  this.activeProjectSource.next(v);
}

get activeProject() {
  return this.activeProjectSource.value;
}

I'm using BehaviorSubject in the service since I want components to get the current value upon subscribing without any change.我在服务中使用BehaviorSubject因为我希望组件在订阅时获得当前值而不做任何更改。 The getter/setter is there because I was doing some other binding directly to a service property, which I've since learned is not recommended. getter/setter 在那里是因为我直接对服务属性进行了一些其他绑定,从那以后我不推荐使用它。

The two sibling components that eventually trace back to a common parent, but I'm not using @Input() or @Output() or any parameter passing in the DOM:最终追溯到共同父级的两个兄弟组件,但我没有使用@Input()@Output()或在 DOM 中传递的任何参数:

this.appProjectService.activeProject$.subscribe(activeProject => {
  this.activeProject = activeProject;
});

Each component is binding to the this.activeProject property in their respective component using [(ngModel)] :每个组件都使用[(ngModel)]绑定到各自组件中的this.activeProject属性:

<input type="checkbox" [(ngModel)]="activeProject.someProperty">

Question问题

If each component obtained what I thought was a copy of activeProject through this.appProjectService.activeProject$.subscribe() , how is it working that a change to the local property in one component is reflected in the other?如果每个组件都通过this.appProjectService.activeProject$.subscribe()获得了我认为是activeProject的副本,那么一个组件中本地属性的更改会反映在另一个组件中是如何工作的? In the end this is the behavior I want, but I can't understand why it works.最后这是我想要的行为,但我不明白为什么它会起作用。 Is there some passing by reference that I'm not understanding in rxjs observables?在 rxjs observables 中是否有一些我不理解的引用传递?

sIf you have 2 components, the both local variables activeProject use the same reference of activeProject . s如果您有 2 个组件,则两个局部变量activeProject使用相同的activeProject引用。 ngModel is bound to a property of this reference. ngModel绑定到此引用的属性。 So it's working, because a change in a component only update the property of the reference, and does not change the reference.所以它起作用了,因为组件中的更改只会更新引用的属性,而不会更改引用。 You can even use a variable activeProject without wrapping it in a BehaviorSubject .您甚至可以使用变量activeProject而不将其包装在BehaviorSubject中。

I know this should be in comment but this much of letters comment won't accept.我知道这应该在评论中,但这么多的信件评论不会接受。

Forget about RxJS for a while.暂时忘记RxJS

Now you have getter and setter for your property.现在你的属性有了gettersetter

You set activeProjectValue in your service.您在服务中设置activeProjectValue

Now when you subscribe it in one component, you will get the object which will be passed by reference.现在,当您在一个组件中订阅它时,您将获得object ,它将通过引用传递。 Same for the other component.其他组件也一样。 As both components accessing same object they are passed by reference.作为访问相同 object 的两个组件,它们通过引用传递。

If you have to break the reference, to use it differently.如果你必须打破参考,以不同的方式使用它。

Also each component obtained what I thought was a copy of activeProject .... this means they copy by refenrence of object.此外each component obtained what I thought was a copy of activeProject副本 .... 这意味着它们通过引用 object 进行复制。

I know, you know how to break reference, but this is just for sake for future viewers我知道,你知道如何打破参考,但这只是为了未来的观众

To break the reference of object you can use JSON.parse(JSON.stringify(*ObjectName*)要打破 object 的引用,您可以使用JSON.parse(JSON.stringify(*ObjectName*)

In your example在你的例子中

this.appProjectService.activeProject$.subscribe(activeProject => {
   this.activeProject = JSON.parse(JSON.stringify(activeProject));
});

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

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