# Two aurelia-store instances between canActivate & bind

**URL:** <https://discourse.aurelia.io/t/two-aurelia-store-instances-between-canactivate-bind/3848>\
**Category:** Framework Knowledge\
**Created:** [October 22, 2020, 5:47pm UTC](https://discourse.aurelia.io/t/two-aurelia-store-instances-between-canactivate-bind/3848 "2020-10-22T17:47:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![davicoradini](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/davicoradini/32/1676_2.png) [@davicoradini](https://discourse.aurelia.io/u/davicoradini)\
**Post date:** [October 22, 2020, 5:47pm UTC](https://discourse.aurelia.io/t/two-aurelia-store-instances-between-canactivate-bind/3848/1 "2020-10-22T17:47:09Z")

</div>

Hi guys,

I’d like to count with your wisdom here.  
I tried to condensate as much as possible my screenshot and question.

I’d like to understand the Dependency Injection behavior. If I use Container.instance.get(Store) in my canActivate method, and than I try to use it the bind method, the instances are not the same, in the Bind method I don’t have my initialState and actions anymore.

My problem was way bigger than that, but I could solve it saving an instance of the Store somewhere in my application.

Am I doing somethign wrong?

 ![two aurelia-store instances](https://canada1.discourse-cdn.com/flex027/uploads/aurelia/original/2X/6/601930563831072a23efb6fc8d91f829a4978bd5.png)

---

<div class="post-metadata">

**Author:** ![avrahamcool](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/avrahamcool/32/368_2.png) [@avrahamcool](https://discourse.aurelia.io/u/avrahamcool)\
**Post date:** [October 22, 2020, 6:24pm UTC](https://discourse.aurelia.io/t/two-aurelia-store-instances-between-canactivate-bind/3848/2 "2020-10-22T18:24:23Z")

</div>

just a question.  
why you don’t use DI in the constructor and save the instance as a class variable to be used later in `canActivate` and `bind`? [using `autoinject`, and not by accessing the Container directly]  
this is the correct way to use DI.

regardless - it should work your way too: maybe you registered the store as transient?

---

<div class="post-metadata">

**Author:** ![zewa666](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/zewa666/32/19_2.png) [@zewa666](https://discourse.aurelia.io/u/zewa666)\
**Post date:** [October 22, 2020, 6:44pm UTC](https://discourse.aurelia.io/t/two-aurelia-store-instances-between-canactivate-bind/3848/3 "2020-10-22T18:44:40Z")

</div>

A common issue is trying to DI request the Store before the plugin did the registration aka in main.ts

---

<div class="post-metadata">

**Author:** ![Krussicus](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/krussicus/32/1605_2.png) [@Krussicus](https://discourse.aurelia.io/u/Krussicus)\
**Post date:** [October 23, 2020, 5:13am UTC](https://discourse.aurelia.io/t/two-aurelia-store-instances-between-canactivate-bind/3848/4 "2020-10-23T05:13:45Z")

</div>

According to [the docs](https://aurelia.io/docs/fundamentals/dependency-injection#object-lifetime-child-containers-and-default-behavior) you should get the same singleton instance by default so I believe your thinking is correct. It may be something peculiar with the Store, assuming you have the standard `initialState` [setup](https://aurelia.io/docs/plugins/store#configuring-your-app).  
Do you get the same result with other lifecycle stages, e.g. `activate`, `attached` instead of `bind`, or doing the first `get` at a later stage, i.e. later than `canActivate`?  
I’d test it myself but I’m sadly without a PC at the moment.  
That “saving an instance” works would suggest that the container is losing the reference somewhere, which, as @avrahamcool mentioned, would indicate that the Store might be transient or that your DI container is getting disposed somewhere between the lifecycle stages and might be picking up your stored instance somehow.  
How are you setting up the Store plugin and what’s your VM class configuration, i.e. does it have the `@transient`decorator, use `determineActivationStrategy()` (which can also be accomplished via the route config), etc.?

---

<div class="post-metadata">

**Author:** ![davicoradini](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/davicoradini/32/1676_2.png) [@davicoradini](https://discourse.aurelia.io/u/davicoradini)\
**Post date:** [October 23, 2020, 2:56pm UTC](https://discourse.aurelia.io/t/two-aurelia-store-instances-between-canactivate-bind/3848/5 "2020-10-23T14:56:47Z")

</div>

Hei guys, thanks to you I fond the problem, I was trying to check your suggestions about the lifecycle and transient, when I found out there was a **`const container = new Container().makeGlobal();`** beeing made in the middle of a file, not a class, something weird, somebody left it there.

I didn’t understand excactly why, but it changes the `Container.instance = this;` in the `aurelia-dependency-injection.js` file.

We use .makeGlobal() it in the Unit Tests, as a suggestion made by someone here in the channel in the past.

Well, I’m sorry I bothered you guys, but this made me go and check further in the code and finally discover the real problem.

Thank you!

---

<div class="post-metadata">

**Author:** ![zewa666](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/zewa666/32/19_2.png) [@zewa666](https://discourse.aurelia.io/u/zewa666)\
**Post date:** [October 24, 2020, 6:07am UTC](https://discourse.aurelia.io/t/two-aurelia-store-instances-between-canactivate-bind/3848/6 "2020-10-24T06:07:30Z")

</div>

MakeGlobal is primarily used in unit tests where you want to create a fresh new root container. Glad you found the issue
