# How to use
This page shows an simple example how to setup and use vuex-rest-api.
# Steps
- Import
vuex-rest-api
(I called itVapi
). - Create a
Vapi
instance.
At least you have to set the base URL of the API you're requesting from. You can also define the default state. If you don't define a default state from a property it will default tonull
. In the example - Create the actions.
Each action represents a Vuex action. If it will be called (propertyaction
), it requests a specific API endpoint (propertypath
) and sets the related property namedproperty
to the response's payload. - Create the store object
- Pass it to Vuex. Continue reading here to know how to call the actions.
// store.js
import Vuex from "vuex"
import Vue from "vue"
// Step 1
import Vapi from "vuex-rest-api"
Vue.use(Vuex)
// Step 2
const posts = new Vapi({
baseURL: "https://jsonplaceholder.typicode.com",
state: {
posts: []
}
})
// Step 3
.get({
action: "getPost",
property: "post",
path: ({ id }) => `/posts/${id}`
})
.get({
action: "listPosts",
property: "posts",
path: "/posts"
})
.post({
action: "updatePost",
property: "post",
path: ({ id }) => `/posts/${id}`
})
// Step 4
.getStore()
// Step 5
export const store = new Vuex.Store(posts)
← Introduction API →