@sparing-software/nuxt-service-plugin
v1.0.2
Published
Services integration with Nuxt.js
Downloads
5
Readme
Nuxt service plugin
Services integration with Nuxt.js
Installation
Install package in your project
$ npm install @sparing-software/nuxt-service-plugin
Configuration
- 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']
}
Usage
- 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()
Contributing
Want to help improve this plugin? Great!
Project is open-source so fork repo and join us!
License
MIT License © Sparing Interactive