In a Vue 3 application with Laravel, it's often necessary to persist data across pages and sessions. One way to achieve this is by using Vuex, a state management library for Vue, and localStorage, a web storage API that allows you to store key-value pairs locally in the user's browser. This guide will walk you through how to use Vuex and localStorage to store and retrieve data across pages in your Vue 3 application with Laravel.
1. Install Vuex and create a store:
npm install vuex
Create store.js in your Project directory, that is, in the same root as App.js. Save the snippet below in the store.js file.
import { createStore } from 'vuex';
export default createStore({
state: {
myData: localStorage.getItem('myData') || ‘No Record!’, // Retrieve data from localStorage if available
},
mutations: {
setMyData(state, data) {
state.myData = data;
localStorage.setItem('myData', data); // Store data in localStorage
},
},
actions: {
updateMyData(context, data) {
context.commit('setMyData', data);
},
},
getters: {
getMyData(state) {
return state.myData;
},
},
});
In the example above, we're initializing the myData state property with the value retrieved from localStorage (if it's available) when the store is created. We're also defining a mutation setMyData that sets the myData value in both the store and localStorage, and an action updateMyData that commits the setMyData mutation.
2. To Import the store and use it in your Vue components, add this to your main.js file:
import { createApp } from 'vue';
import store from './store';
import App from './App.vue';
const app = createApp(App);
app.use(store);
app.mount('#app');
In the example above, we're importing the store and using the app.use() method to install it in the Vue application. This makes the store available to all components within the application.
3. To use the store in your Vue components:
// MyComponent.vue
<template>
<div>
<p>{{ myData }}</p>
<button @click="updateData">Update Data</button>
</div>
</template>
<script>
export default {
computed: {
myData() {
return this.$store.getters.getMyData;
},
},
methods: {
updateData() {
this.$store.dispatch('updateMyData', ‘Your New data');
},
},
};
</script>
We're using the store in a Vue component by defining a computed property myData that retrieves the myData value from the store using the getMyData getter, and a method updateData that dispatches the updateMyData action with a new value.
4. In Laravel, you can store data in localStorage using JavaScript in your blade templates:
<!-- my-view.blade.php -->
@extends('layouts.app')
@section('content')
<div id="app">
<my-component></my-component>
</div>
<script>
localStorage.setItem('myKey', 'myValue');
</script>
@endsection
We set a value in localStorage using JavaScript in the blade template. This value can be retrieved later in other blade templates or in the Vue application using localStorage.getItem('myKey').
By combining Vuex and localStorage, you can provide a seamless experience for your users by storing and retrieving data across pages and sessions.
Share Post