# Reusing view model for different routes

**URL:** <https://discourse.aurelia.io/t/reusing-view-model-for-different-routes/866>\
**Category:** Help Requests\
**Created:** [March 5, 2018, 5:23pm UTC](https://discourse.aurelia.io/t/reusing-view-model-for-different-routes/866 "2018-03-05T17:23:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krossmaskinen](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/krossmaskinen/32/355_2.png) [@Krossmaskinen](https://discourse.aurelia.io/u/Krossmaskinen)\
**Post date:** [March 5, 2018, 5:23pm UTC](https://discourse.aurelia.io/t/reusing-view-model-for-different-routes/866/1 "2018-03-05T17:23:31Z")

</div>

Hi!

I have two routes with different views but the same view model. How can I specify what view the route should use?

For example, I have route `/person` and `/user`. The both use the same view model because the data is identical. But the presentation (view) should be different.

I’ve tried passing parameters in the route and then setting the view in `getViewStrategy()`, but there seems to be no way of getting any info from the `router.currentInstruction` in time before `getViewStrategy()` is called.

`created()` is the lifecycle hook where the `router.currentInstruction` is updated, but `getViewStrategy()` is run before that, so I can’t get any info from the route before it’s too late.

I can’t use the `<compose>` without an ugly workaround, which would require some sort of a middle view. So I prefer doing it as non-ugly as possible.

Is there a way to specify the view for a route the same way you can specify the view model?

---

<div class="post-metadata">

**Author:** ![NickShallee](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/nickshallee/32/591_2.png) [@NickShallee](https://discourse.aurelia.io/u/NickShallee)\
**Post date:** [March 5, 2018, 7:23pm UTC](https://discourse.aurelia.io/t/reusing-view-model-for-different-routes/866/2 "2018-03-05T19:23:40Z")

</div>

Seems like a good candidate for `<compose>`. Out of curiosity what do you mean by “without an ugly workaround, which would require some sort of a middle view?”

---

<div class="post-metadata">

**Author:** ![Krossmaskinen](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/krossmaskinen/32/355_2.png) [@Krossmaskinen](https://discourse.aurelia.io/u/Krossmaskinen)\
**Post date:** [March 6, 2018, 8:37am UTC](https://discourse.aurelia.io/t/reusing-view-model-for-different-routes/866/3 "2018-03-06T08:37:24Z")

</div>

It feels like I would need a separate view that contains the `<compose>` element that in turn references the real view. Or is this one of the moments I should use InlineViewStrategy instead?

---

<div class="post-metadata">

**Author:** ![austinh](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@austinh](https://discourse.aurelia.io/u/austinh)\
**Post date:** [March 6, 2018, 11:56am UTC](https://discourse.aurelia.io/t/reusing-view-model-for-different-routes/866/4 "2018-03-06T11:56:41Z")

</div>

Instead of fighting router with 2 views on same view model, you can feed router what it wants: 2 view models without code duplication.

```auto
export class Person extends User {}

```

---

<div class="post-metadata">

**Author:** ![Alexander-Taran](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/alexander-taran/32/2308_2.png) [@Alexander-Taran](https://discourse.aurelia.io/u/Alexander-Taran)\
**Post date:** [March 6, 2018, 11:59am UTC](https://discourse.aurelia.io/t/reusing-view-model-for-different-routes/866/5 "2018-03-06T11:59:27Z")

</div>

> [@austinh](#):
>
> 2 view models without code duplication.

Can be tricky with decorators. There were some issues with this kind of inheritance.  
Can you confirm that you used it and everything was ok?

---

<div class="post-metadata">

**Author:** ![austinh](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@austinh](https://discourse.aurelia.io/u/austinh)\
**Post date:** [March 6, 2018, 12:02pm UTC](https://discourse.aurelia.io/t/reusing-view-model-for-different-routes/866/6 "2018-03-06T12:02:12Z")

</div>

Yes, I am using it both on route module and normal module. At least @inject and @bindable (irrelevant in route module) works out of the box. Not tested with @autoInject, since I don’t use TypeScript.

---

<div class="post-metadata">

**Author:** ![Krossmaskinen](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/krossmaskinen/32/355_2.png) [@Krossmaskinen](https://discourse.aurelia.io/u/Krossmaskinen)\
**Post date:** [March 6, 2018, 12:14pm UTC](https://discourse.aurelia.io/t/reusing-view-model-for-different-routes/866/7 "2018-03-06T12:14:25Z")

</div>

Interesting. I remember in the “Intermediate Aurelia Development” held by Rob on Vimeo, he said “don’t use inheritance for this sort of thing.”.

However if this works, I’m happy to go with it for the time being.

I’ll try it out and update here later.

Thx!

---

<div class="post-metadata">

**Author:** ![austinh](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@austinh](https://discourse.aurelia.io/u/austinh)\
**Post date:** [March 6, 2018, 12:30pm UTC](https://discourse.aurelia.io/t/reusing-view-model-for-different-routes/866/8 "2018-03-06T12:30:12Z")

</div>

> [@Krossmaskinen](#):
>
> I remember in the “Intermediate Aurelia Development” held by Rob on Vimeo, he said “don’t use inheritance for this sort of thing.”.

Well, I didn’t see the course. Maybe I should pay for it, in order to find out some of my bad practice 😬

---

<div class="post-metadata">

**Author:** ![Krossmaskinen](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/krossmaskinen/32/355_2.png) [@Krossmaskinen](https://discourse.aurelia.io/u/Krossmaskinen)\
**Post date:** [March 6, 2018, 12:34pm UTC](https://discourse.aurelia.io/t/reusing-view-model-for-different-routes/866/9 "2018-03-06T12:34:34Z")

</div>

It’s really good! Only found two things that are outdated:

All the aurelia UI stuff has been renamed to `ux`, instead fo `ai`, like `ux-dialog` instead of `ai-dialog`. And the chain method to `showDialog()` is now `whenClosed()`, not `then()`.

---

<div class="post-metadata">

**Author:** ![jeffgrann](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/jeffgrann/32/43_2.png) [@jeffgrann](https://discourse.aurelia.io/u/jeffgrann)\
**Post date:** [March 6, 2018, 12:46pm UTC](https://discourse.aurelia.io/t/reusing-view-model-for-different-routes/866/10 "2018-03-06T12:46:57Z")

</div>

I **highly** recommend Rob’s Beginning and Intermediate courses on Vimeo! Well worth the price of admission.
