Skip to content

基于ts封装的依赖注入,适用于ts项目,方便快捷的实现类与类之间的依赖关系,避免代码中重复的new实例化

Notifications You must be signed in to change notification settings

wenzirang/TS-Injector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

目录结构

├───Injector    # 依赖注入的核心代码          
├───service		# 使用实例      

装饰器注解

@Injectable() :此装饰器用于标识此类为一个可以注入的服务;列子:
export class HttpService{
	constructor(){
        console.log('我是一个已经被装饰标识为一个可以注入的服务了')
	}
    
    // get方法
    public getData(url, params, header?) {
        const option = this.getOption({ params: params }, header)
        return new Promise(function (resolve, reject) {
            axios.get(url, option)
                .then(function (res) {
                    resolve(res.data);
                })
                .catch(function (err) {
                    reject(err.data);
                });
        });
    }
}
@Inject 此装饰器用于标识使用此装饰器的变量 为一个注入到当前类的服务可以直接使用他的实例
import {Inject} from "./Injector/Inject";
import {HttpService} from './http'
export class TestService{
	// 此处的http变量是已经注入进来的服务了 可以直接使用了
	@Inject private http : HttpService
    constructor(){        
        //使用注入进来的服务
        this.http.getData();
    }
}

About

基于ts封装的依赖注入,适用于ts项目,方便快捷的实现类与类之间的依赖关系,避免代码中重复的new实例化

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published