Skip to content

Instantly share code, notes, and snippets.

@justinyoo
Created March 22, 2017 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinyoo/0a6283d07b43f477387d4d1acf1f0d67 to your computer and use it in GitHub Desktop.
Save justinyoo/0a6283d07b43f477387d4d1acf1f0d67 to your computer and use it in GitHub Desktop.
Using IoC Container in Vue.js and TypeScript App
// Ninja.vue - Updated
<script lang="ts">
import Vue from "vue";
// Imports both Component and Inject decorators from vue-property-decorator,
// instead of vue-class-component
import { Component, Inject } from "vue-property-decorator";
import { Container } from "inversify";
import SERVICE_IDENTIFIER from "../models/Identifiers";
import { Ninja as _Ninja } from "../models/Warrior";
@Component({
name: "Ninja"
})
export default class Ninja extends Vue {
warrior: string;
weapon: string;
// IoC container provided from App.ts is injected here
@Inject(SERVICE_IDENTIFIER.CONTAINER)
private _container: Container;
private _ninja: _Ninja;
created (): void {
this._ninja = this._container.get<_Ninja>(SERVICE_IDENTIFIER.WARRIOR);
this.warrior = this._ninja.name;
this.weapon = this._ninja.weapon.name;
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment