Vuex

statemanagement with

State of a single

Vue component

Single Vue component

State of a single Vue component

one-way data flow

State, data property. Actions, methods property.

View,  template property.

Share data

between components

parent to child

Parent to Child

Parent to Child

<template>
    <h1>{{ value }}</h1>
</template>

<script>
export default {
  name: "counter",
  props: ["value"]
};
</script>

<style>
// ...
</style>
<template>
  <div id="app">
    <counterValue :value="count"></counterValue>
    <button v-on:click="increment">increment</button>
    <button v-on:click="decrement">decrement</button>
  </div>
</template>

<script>
    // ...
    // state
    data() {
        return {
            count: 2
        };
    },
    // ...
</script>

<style>
// ...
</style>

Share data

between components

child to parent

Child to Parent

<template>
    <h1>{{ value }}</h1>
</template>

<script>
export default {
  name: "counter",
  props: ["value"]
};
</script>

<style>
// ...
</style>
<template>
  <div id="app">
    <counterValue :value="count"></counterValue>
    <button v-on:click="increment">increment</button>
    <button v-on:click="decrement">decrement</button>
  </div>
</template>

<script>
    // ...
    // state
    data() {
        return {
            count: 2
        };
    },
    // ...
</script>

<style>
// ...
</style>

Child to Parent

Updating multiple

Vue components

Challenges

  • Sharing state with multiple components
  • Updating multiple components based on a shared state
  • Changing data in the state

Updating multiple

Vue components
with Vuex

Multiple vue components state

Flow

  1. Vue component does a dispatch to an action.
  2. The action does a get request.
  3. Calls the mutation.
  4. Mutation will merge old data with new.
  5. Now the state has changed. By the mutation.
  6. New data is rendered in all vue components that are attached.

Vuex

By CodePamoja

Vuex

  • 19