Using Object Oriented Programming, OOP, style allows us to apply Inversion of Control, IoC, and more patterns. An IoC container helps decoupling dependencies by using a class constructor or properties to identify and inject its dependencies. This lesson will show you how can you create and use an IoC container to decouple an http client and users service dependencies in a Vue component with TypeScript and InversifyJS.

When you are using Class, you also need to mantain the singleton of Class. Use can use similiar approach as Angular in VueJS as well.

Install:

npm i --save inversify reflect-metadata inversify-inject-decorators

Modify tsconfig.json:

{
"compilerOptions": {
"lib": [
"dom",
"es5",
"es2015"
],
"module": "es2015",
"moduleResolution": "node",
"target": "es5",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true
}
}

main.ts:

import "reflect-metadata"
import "core-js/es6/map"
import "core-js/es6/symbol" import Vue from 'vue'
import App from './App.vue' Vue.config.productionTip = false /* eslint-disable no-new */
new Vue({
el: '#app',
template: '<App/>',
components: { App }
})

Tip: If you want to create unqiue String, you can use Symbol('UserService'), it is useful tool.

Create a container.ts, which contains all the singleton services:

import {Container} from 'inversify';
import {UsersService} from './user-service';
import {HttpClient} from './http-client';
import {TYPES} from './types';
import getDecorators from 'inversify-inject-decorators'; const container = new Container(); container.bind<UserService>(TYPES.UsersService).to(UsersService);
container.bind<HttpClient>(TYPES.HttpClient).to(HttpClient); // Lazy inject is good for props
const {lazyInject} = getDecorators(container); export {container, lazyInject}

Create types.ts:

export const TYPES = {
UsersService: Symbol('UsersService'),
HttpClient: Symbol('HttpClient')
}

http-client.ts:

From:

export class HttpClient {
get(url: string): Promise<any> {
return fetch(url).then(data => data.json())
}
}

To:

import {injectable} from 'inversify';

@injectable()
export class HttpClient {
get(url: string): Promise<any> {
return fetch(url).then(data => data.json())
}
}

user-service.ts:

From:

export class UsersService {
private http: HttpClient; constructor() {
this.http = new HttpClient()
} getUsers(): Promise<any> {
return this.http.get('https://jsonplaceholder.type');
}
}

To:

import {inject, injectable} from 'inversify';
import {TYPES} from './types'; @injectable()
export class UsersService { constructor(@inject(TYPES.HttpClient) private http) {
} getUsers(): Promise<any> {
return this.http.get('https://jsonplaceholder.type');
}
}

Then we can use UsersService in App.vue:

<script lang="ts">
import Vue from 'vue'
import Component from 'vue-class-component'
import { lazyInject } from './container'
import { TYPES } from './types'
@Component
export default class App extends Vue {
users = []
@lazyInject(TYPES.UsersService)
usersService
created() {
this.usersService.getUsers()
.then(data => {
this.users = data
})
}
}
</script>

最新文章

  1. 在idea中maven项目jdk编译version总是跳到1.5
  2. JS高程3.基本概念(2)
  3. android源码framework下添加新资源的方法
  4. mybatis的批量删除
  5. AC自动机题目汇总
  6. PL301 matrix内部模块
  7. loadrunner个版本历程
  8. SQL Agent Job -&gt;&gt; 通过sys.sysprocesses的program_name字段来定位对应的Job
  9. dbutils 执行sql返回的数据类型
  10. 字体在Android View中的输出 drawText
  11. java循环、数组练习
  12. 201521123074 《Java程序设计》第7周学习总结
  13. TomCat的安装及测试
  14. Perl的列表和数组
  15. oracle 外连接以及用on和where 的区别
  16. js点滴
  17. 使用Python监控Linux系统
  18. SCCM2012 R2实战系列之十二:解决OSD分发时间过长的问题
  19. [leetcode]270. Closest Binary Search Tree Value二叉搜索树中找target的最接近值
  20. C#.NET中遍历指定目录下的文件(及所有子目录及子目录里更深层目录里的文件)

热门文章

  1. 【译】x86程序员手册35-9.8异常条件
  2. iOS UI 顶级布局
  3. Oracle11g 审计介绍
  4. ALTER TABLE - 修改表的定义
  5. vue工程化之项目引入jquery
  6. SQLSTATE=2300
  7. Python3中assert断言
  8. 第2节 hive基本操作:9、hive当中创建外部表的语法及外部表的操作&amp;分区表的语法和操作
  9. rownum导致sql不能进行谓词推入
  10. HashTable的C++实现