16. Project [vuejs-14]: Make the session responsive
We saw that the [session] object used in the previous project had non-reactive properties: if you modify them, the views that use those properties are not updated. It is possible to have a reactive [session] object if you store it in the reactive data of the views. This is demonstrated in the [vuejs-14] project.
The project tree is as follows:

16.1. The [session] object
The [session] object shared by all components remains unchanged.
16.2. The [./plugins/pluginSession] plugin
The script [pluginSession] remains unchanged. The shared object [session] is available in the property [$session] of all components.
16.3. The main script [main.js]
The main script [main.js] remains unchanged.
16.4. The main view [App]
The view [App] 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 {
// name
name: "app",
// components
components: {
Table
},
// life cycle
created() {
// simulation table generation
this.$session.generateLignes();
}
};
</script>
Comments
- Line 9: The [Table] component no longer emits the [updateTable] event, which requests that the [Table] component be regenerated. As a result, the [updateTable] method has been removed;
16.5. The [Table] component
The [Table] component has changed as follows:
<template>
<div>
<!-- empty list -->
<template v-if="lignes.length==0">
<b-alert show variant="warning">
<h4>Votre liste de simulations est vide</h4>
</b-alert>
<!-- reload button-->
<b-button variant="primary" @click="rechargerListe">Recharger la liste</b-button>
</template>
<!-- non-empty list-->
<template v-if="lignes.length!=0">
<b-alert show variant="primary" v-if="lignes.length==0">
<h4>Liste de vos simulations</h4>
</b-alert>
<!-- simulation table -->
<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 property [session] on line 45. Placed here, the property [session] 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

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