screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <div id="app" v-if="isDataStored"> <h3>Vue js Get item(Object) from Session Storage</h3> <table> <thead> <tr> <th>River Name</th> <th>Length (km)</th> </tr> </thead> <tbody> <tr v-for="river in rivers" :key="river.name"> <td>{{ river.name }}</td> <td>{{ river.length }}</td> </tr> </tbody> </table> </div> <script type="module"> const app = new Vue({ el: "#app", data: { isDataStored: false // add this property }, mounted() { // Create an example array of river objects const rivers = [ { name: 'Amazon River', length: 6575 }, { name: 'Nile River', length: 6650 }, { name: 'Yangtze River', length: 6300 }, { name: 'Mississippi River', length: 6275 }, { name: 'Yenisei River', length: 5549 } ]; // Stringify and store the array in session storage sessionStorage.setItem('rivers', JSON.stringify(rivers)); this.isDataStored = true; }, computed: { rivers() { // Get the array of river objects from session storage const storedRivers = sessionStorage.getItem('rivers'); // Parse the array from storage return JSON.parse(storedRivers); } } }) </script> <style scoped> #app { margin: 0 auto; max-width: 800px; padding: 20px; } /* style for the table */ table { border-collapse: collapse; width: 100%; } th, td { text-align: left; padding: 8px; } th { background-color: #ddd; } tr:nth-child(even) { background-color: #f2f2f2; } /* style for the heading */ h3 { font-size: 1.5rem; font-weight: bold; margin-top: 0; } </style> </body> </html>