Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Vue.js 3 Cookbook

You're reading from   Vue.js 3 Cookbook Discover actionable solutions for building modern web apps with the latest Vue features and TypeScript

Arrow left icon
Product type Paperback
Published in Sep 2020
Publisher Packt
ISBN-13 9781838826222
Length 562 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Heitor Ramon Ribeiro Heitor Ramon Ribeiro
Author Profile Icon Heitor Ramon Ribeiro
Heitor Ramon Ribeiro
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Understanding Vue 3 and Creating Components 2. Introducing TypeScript and the Vue Ecosystem FREE CHAPTER 3. Data Binding, Form Validations, Events, and Computed Properties 4. Components, Mixins, and Functional Components 5. Fetching Data from the Web via HTTP Requests 6. Managing Routes with vue-router 7. Managing the Application State with Vuex 8. Animating Your Application with Transitions and CSS 9. Creating Beautiful Applications Using UI Frameworks 10. Deploying an Application to Cloud Platforms 11. Directives, Plugins, SSR, and More 12. Other Books You May Enjoy

Creating the Vuex state

In order to save the data inside of Vuex, you need to have an initial state that is loaded with the application and defined as the default one when the user enters your application. Here, we are going to learn how to create the Vuex state and use it as a singleton, so that Vuex can be used in an SPA and an SSR page:

Now we will create a Vuex store that can be used in an SSR and an SPA:

  1. In the src/store folder, create a new folder called user, and inside this folder create a new file named state.js.
  2. Create a new generateState function. This function will return a JavaScript object, with three main properties, data, loading, and error. The data property will be a JavaScript object, with a property called usersList defined as an empty array as default, and a property called userData with the default object of a user. The loading property will be a boolean, set to false by default, and error will have a default value initializing to null:
const generateState = (...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image