Skip to content

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nuxt service plugin

MIT license Downloads number JavaScript Style Guide

Services integration with Nuxt.js

Installation

Install package in your project

$ npm install @sparing-software/nuxt-service-plugin

Configuration

  1. 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)
}
  1. Add plugin (nuxt.config.js)
plugins: [
  '@/plugins/service'
]
  1. Transpile plugin with webpack to autoimport services files (nuxt.config.js)
build: {
  transpile: ['@sparing-software/nuxt-service-plugin']
}

Usage

  1. Create service folder in your root directory
  2. Add service by creating js file - for example: @/service/books.js will create books service
export default axios => ({
  getAll () {
    return axios.get('/books')
  },
  getById (id) {
    return axios.get('/books?id=' + id)
  }
})
  1. 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

About

Services integration with Nuxt.js

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages