0

How would be perfect to organize data and interaction between two vuejs components? For example: 1) i have one component

item(v-for="item in items)
 a {{item.name}}

2) and the second

card(v-for="item in items")
 div.content
  img {{item.photo}}
  div {{item.desc}}
  button Details

The main idea is when i click on list item i want to toggle the card with the same id, as list has. I use one file conponent management from vue webpack template.

1 Answer 1

6

A lot of folk seem to be trying to use Vue without a store. Could you be one of them? Perhaps because the store is not strictly part of Vue? Perhaps because the docs spend more time on parent-child communication, events etc (complicated) than on state management (simple)? Perhaps because OO has rotted our brains?

Vue wants to talk to a store. The whole point of bidirectional binding is to separate state from markup. The whole reason why this is such a genius idea is that many (most?) items of state have more than one representation on screen, like your items array. Your store, which can be as simple as a js object in window scope, should contain all your page state at a given moment. You should be able to copy-paste the store between pages, and be looking at the same thing on screen. The important qualities of a store are...

  • that there's only one of them.
  • that you 'normalize' your store so that an item of state is only stored once, and there are minimal dependencies between items of state.

Your items array should be in the store, and both components refer to this 'single source of truth'. If you're using other people's components, then you'll need to feed them some properties, but properties are for creating tunnels through Vue components to link leaf components with your store. Your items are your state, and state shouldn't generally live in Vue stuff. Does that help?

Sign up to request clarification or add additional context in comments.

4 Comments

this is an interesting comment. I always thought Vue's $emit and $on were were very myopic. Like pubsub confined to parent / child OR ancestor/descendant hierarchies seemed very limiting. I had even asked this at so: stackoverflow.com/questions/47366103/vue-component-on-use-case. But, the deeper question now is why does Vue as a framework not document / stress on state management ? WDYT ...
@82Tuskers. The Vue docs are generally marvellous, and everything is there, but they don't help you prioritise one feature relative to another. The simple example of state management starts with const, so anyone who's suspicious of const will switch off, or block on that. This issue comes up so frequently that I agree, it would really be worth insisting in the docs a bit harder.
My favourite recent example of is here. This potentially complicated problem became simple when you got the store right. You might also be interested in this issue, for me an example of the knots folks get into without a store, or the comments thread of this answer.
💯M discovering this the hard way. I have started a couple of Vue projects thinking that they were 'too small' to bother with store. 👎🏾Now I am rebooting those projects with Vuex! 😫

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.