Services integration with Nuxt.js
Install package in your project
$ npm install @sparing-software/nuxt-service-plugin
- Create
@/plugins/service.js
- feel free to use your favourite http client
import createService from '@sparing-software/nuxt-service-plugin'
export default function (ctx, inject) {
const httpClient = ctx.$axios
const service = createService(httpClient)
ctx.$service = service
inject('service', service)
}
- Add plugin (
nuxt.config.js
)
plugins: [
'@/plugins/service'
]
- Transpile plugin with webpack to autoimport services files (
nuxt.config.js
)
build: {
transpile: ['@sparing-software/nuxt-service-plugin']
}
- Create
service
folder in your root directory - Add service by creating js file - for example:
@/service/books.js
will createbooks
service
export default axios => ({
getAll () {
return axios.get('/books')
},
getById (id) {
return axios.get('/books?id=' + id)
}
})
books
service is now accessible all over the app:
- Components:
this.$service.books.getAll()
- Vuex actions:
this.app.$service.books.getAll()
- The Context:
ctx.$service.books.getAll()
Want to help improve this plugin? Great!
Project is open-source so fork repo and join us!
MIT License © Sparing Interactive