@scandltd/vue-injector

Dependency Injection for Vue.js 2
1,557 20
Install
npm install @scandltd/vue-injector@next
Latest Version:4.0.3
Vue:3
Older releases:2
License:GPL-2.0
Last Updated:Dec 11, 2022
Links: GitHub  ·  npm
Maintainer: scandltd

vue-injector

Dependency Injection for Vue.js.

This is vue-injector which works only with Vue 2. For the Vue 3 see the next version.

Build Status Coverage Status Size Downloads Version License

Introduction

Vue Injector — Dependency Injection library for Vue.js. Includes the following:

  • Dependency injection for components
  • Construction of the injected services
  • Accessibility of Vue application from a service
  • Utilization of decorators for convenient operation

Get started with the documentation, or play with the examples (see how to run them below).

Live Playground

For examples of the plugin in action, go to codesandbox.

Install

$ npm install @scandltd/vue-injector core-js

:warning: ECMAScript stage 1 decorators. If you use Babel, @babel/plugin-proposal-decorators is needed. If you use TypeScript, enable --experimentalDecorators and --emitDecoratorMetadata flags.

Vue-injector requires a modern JavaScript engine with support for:

If your environment doesn't support one of these you will need to import a shim or polyfill.

// polifill.js

import 'core-js/features/reflect';
import 'core-js/features/promise';
import 'core-js/features/map';

:warning: The reflect polyfill should be imported only once in your entire application because the Reflect object is meant to be a global singleton.

Example

This is a small example of using the vue-injector to create an http service using Observables:

// component/todoList.js

/** ... */

/** 
 *  Class-style Vue components:
 *  
 *  @Component
 *  class TodoListComponent extends Vue {
 *    @Inject(Http) httpClient;
 *  }
 *  
 *  
 *  Typescript:
 *  
 *  @Component
 *  class TodoListComponent extends Vue {
 *    @Inject httpClient: Http;
 *  }
 *  
 */

import Http from '../services/http';

export default {
  name: 'TodoList',
  providers: {
    httpClient: Http
  },
  created() {
    this.httpClient
      .get(URL)
      /** any pipes */
      .subscribe(
        this.taskHandler
      )
  },
  methods: {
    taskHandler(tasks) {
      /** ... */
    }
  }
}

/** ... */

// services/setup.js

import Vue from 'vue';
import { VueInjector } from '@scandltd/vue-injector';

Vue.use(VueInjector);

export default new VueInjector();

// main.js

import injector from './services/setup';

/** ... */

const root = new Vue({
  /** ... */
  injector
});

// services/client.js

import axios from 'axios';
import { Injectable } from '@scandltd/vue-injector';

@Injectable({
  useFactory: () => axios.create(/** ... */)
})
class Client {}

export default Client;

// services/http.js

import { Injectable, Inject } from '@scandltd/vue-injector';
import * as Observable from 'rxjs/internal/observable/fromPromise';
import { map } from 'rxjs/operators';

import Client from './Client';

@Injectable
class Http {
  @Inject(Client) client;

  observableFactory(promise) {
    return Observable
      .fromPromise(promise)
      .pipe(
        map(({ data }) => data)
      );
  }
  
  /** ... */

  get(url, params) {
    return this.observableFactory(
      this.client.get(url, { params })
    );
  }
}

export default Http

Development Setup

# install deps
yarn install

# build dist files
yarn build

# serve examples at localhost:8080
yarn dev

# lint & run all tests
yarn test

# serve docs at localhost:8080
yarn docs

License

GPL-2.0

Copyright (c) 2018-present Scandltd

Related Packages

inversify-props

Wrapper to use inversify as property decorator with TypeScript, useful for compo...

180,633 87
vue-di-container

Dependency injection container for Vue

1,561 1
@kaokei/use-vue-service

Lightweight Vue 3 state management with dependency injection, inspired by Angula...

1,891 5
@vue-ioc/core

IoC and DI for Vue powered by InversifyJS and inspired by Angular @Module syntac...

786 44
vdi

vue3 Dependency injection And some small functions

1,422 17

Version History

Version Released Vue License
4.0.3 ^3.1.1 GPL-2.0
4.0.2 ^3.1.1 GPL-2.0
4.0.1 ^3.1.1 GPL-2.0
4.0.0 ^3.0.11 GPL-2.0
3.4.1 ^2.6.10 GPL-2.0