Skip to content

16. Project [vuejs-14]: Making the session reactive

We saw that the [session] object used in the previous project had non-reactive properties: if we modify them, the views using these properties are not updated. It is possible to have a reactive [session] object if we store it in the reactive data of the views. This is what the [vuejs-14] project demonstrates.

The project directory structure is as follows:

Image

16.1. The [session] object

The [session] object shared by all components does not change.

16.2. The [./plugins/pluginSession] plugin

The [pluginSession] script does not change. The shared [session] object is available in the [$session] property of all components.

16.3. The main script [main.js]

The main script [main.js] remains unchanged.

16.4. The main view [App]

The [App] view is now as follows:


<template>
  <div class="container">
    <b-card>
      <!-- message -->
      <b-alert show variant="success" align="center">
        <h4>[vuejs-14] : utilisation d'un objet partagé entre composants</h4>
      </b-alert>
      <!-- table HTML -->
      <Table/>
    </b-card>
  </div>
</template>
 
<script>
import Table from "./components/Table";
export default {
  // nom
  name: "app",
  // composants
  components: {
    Table
  },
  // cycle de vie
  created() {
    // génération du tableau des simulations
    this.$session.generateLignes();
  }
};
</script>

Comments

  • line 9: the [Table] component no longer emits the [updateTable] event, which triggers the [Table] component to be regenerated. As a result, the [updateTable] method has been removed;

16.5. The [Table] component

The [Table] component evolves as follows:


<template>
  <div>
    <!-- liste vide -->
    <template v-if="lignes.length==0">
      <b-alert show variant="warning">
        <h4>Votre liste de simulations est vide</h4>
      </b-alert>
      <!-- bouton de rechargement-->
      <b-button variant="primary" @click="rechargerListe">Recharger la liste</b-button>
    </template>
    <!-- liste non vide-->
    <template v-if="lignes.length!=0">
      <b-alert show variant="primary" v-if="lignes.length==0">
        <h4>Liste de vos simulations</h4>
      </b-alert>
      <!-- tableau des simulations -->
      <b-table striped hover responsive :items="lignes" :fields="fields">
        <template v-slot:cell(action)="row">
          <b-button variant="link" @click="supprimerLigne(row.index)">Supprimer</b-button>
        </template>
      </b-table>
    </template>
  </div>
</template>
 
<script>
export default {
  // calculated state
  computed: {
    lignes() {
      return this.session.lignes;
    }
  },
  // inner state
  data() {
    return {
      fields: [
        { label: "#", key: "id" },
        { label: "Marié", key: "marié" },
        { label: "Nombre d'enfants", key: "enfants" },
        { label: "Salaire", key: "salaire" },
        { label: "Impôt", key: "impôt" },
        { label: "", key: "action" }
      ],
      session : {}
    };
  },
  // life cycle
  created(){
    this.session=this.$session
  },
  // methods
  methods: {
    supprimerLigne(index) {
      // eslint-disable-next-line
      console.log("Table supprimerLigne", index);
      // delete the line
      this.session.deleteLigne(index);
    },
    // reload displayed list
    rechargerListe() {
      // eslint-disable-next-line
      console.log("Table rechargerListe");
      // the list of simulations is generated
      this.session.generateLignes();
    }
  }
};
</script>

Comments:

  • The new feature is in lines 49–51: when the view is created, the session [this.$session] is stored in the [session] property on line 45. Placed here, the [session] property is reactive;
  • Lines 58 and 65: Instead of using [this.$session] to add or remove a row from the table, we use the reactive property [this.session];

16.6. Running the project

Image

We get the same results as in the [vuejs-12] project.