Installing Vuex

Vuex is an NPM package that can be installed from the command line:

$ npm i --save-dev vuex

We will put our Vuex configuration into a new module file store.js:

$ touch resources/assets/js/store.js

We need to import Vuex in this file and, like Vue Router, install it with Vue.use. This gives special properties to Vue that make it compatible with Vuex, such as allowing components to access the store via this.$store.

resources/assets/js/store.js:

import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);

export default new Vuex.Store();

We will then import the store module in our main app file, and add it to our Vue instance.

resources/assets/js/app.js:

...

import router from './router';import store from './store';

var app ...

Get Full-Stack Vue.js 2 and Laravel 5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.