vue router and vuex

Vue-router and Vuex in vue app

Vue-router and Vuex in vue app

Vue-router and Vuex are two important libraries that can greatly enhance the functionality of a Vue application. Vue-router allows for routing and navigation within a Vue application, while Vuex provides centralized state management. In this blog post, we will explore how to use these libraries in a Vue application.

Installing Vue-router and Vuex

First, we need to install these libraries using npm. Open a terminal and navigate to your Vue application directory, then run the following commands:

                                
npm install vue-router vuex --save
                                
                            

This will install Vue-router and Vuex in your application and save them to your package.json file.

Using Vue-router

Vue-router allows you to define routes and navigate between them in your Vue application. To use Vue-router, you need to create a router instance and define your routes.

In your main.js file, import Vue-router and create a new router instance:

                                
import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App.vue'
import Home from './components/Home.vue'
import About from './components/About.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', component: Home },
  { path: '/about', component: About },
]

const router = new VueRouter({
  routes // short for `routes: routes`
})

new Vue({
  router,
  render: h => h(App),
}).$mount('#app')
                                
                            

In the code above, we imported Vue-router and used the Vue.use() method to install it. We then defined our routes using an array of objects, where each object represents a route. Each route object has a path property and a component property, which specifies the component to be displayed when that route is accessed.

Finally, we created a new Vue-router instance and passed in our routes as an option. We then passed our router instance to our root Vue instance using the router option.

Now that we have our router set up, we can use Vue-router's router-link component to navigate between our routes in our Vue templates.


<template>
  <div>
    <nav>
        <router-link to="/">Home</router-link>
        <router-link to="/about">About</router-link>
    </nav>
    <router-view></router-view>
  </div>
</template>

                            

In the code above, we used the router-link component to create links to our routes. The to prop on router-link specifies the path to navigate to. We also used the router-view component to display the component associated with the current route.

Using Vuex

Vuex is a state management library for Vue applications. It provides a centralized store for managing the state of your application.

To use Vuex, we first need to create a store. In your store.js file, create a new Vuex store:

                                
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    }
  },
  actions: {
    incrementAsync ({ commit }) {
      setTimeout(() => {
        commit('increment')
      }, 1000)
    }
  },
  getters: {
    getCount: state => state.count
  }
})

                                
                            

In the code above, we imported Vuex and used the Vue.use() method to install it. We then created a new Vuex store using the new Vuex.Store() method. Our store has three properties: state, mutations, and actions.

The state property is where we define our application's state. In this case,

Things we make