{"id":18925016,"url":"https://github.com/kouyjes/injector-ioc","last_synced_at":"2026-04-13T00:33:25.476Z","repository":{"id":57274038,"uuid":"96528985","full_name":"kouyjes/injector-ioc","owner":"kouyjes","description":"service factory injector angular ","archived":false,"fork":false,"pushed_at":"2019-05-20T02:51:03.000Z","size":47,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-02-01T14:37:10.329Z","etag":null,"topics":["angular","factory","factory-injector","factory-pattern","instance-manage","invoke","method-override","object-creater","react","service","service-injector","vue"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kouyjes.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-07T10:42:01.000Z","updated_at":"2019-05-20T02:51:04.000Z","dependencies_parsed_at":"2022-09-11T06:51:45.659Z","dependency_job_id":null,"html_url":"https://github.com/kouyjes/injector-ioc","commit_stats":null,"previous_names":["kouyjes/injector-manager"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kouyjes%2Finjector-ioc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kouyjes%2Finjector-ioc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kouyjes%2Finjector-ioc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kouyjes%2Finjector-ioc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kouyjes","download_url":"https://codeload.github.com/kouyjes/injector-ioc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239921844,"owners_count":19718844,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angular","factory","factory-injector","factory-pattern","instance-manage","invoke","method-override","object-creater","react","service","service-injector","vue"],"created_at":"2024-11-08T11:08:54.928Z","updated_at":"2025-12-12T04:02:37.293Z","avatar_url":"https://github.com/kouyjes.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Injector 依赖注入会帮助我们创建Service、Factory、Provider，自动创建依赖并注入\n## 起步\n```html\n    \u003cscript src=\"dist/injector.js\"\u003e\u003c/script\u003e\n```\n### 初始化Injector\n```javascript\n    var injector = new HERE.Injector();\n```\n### 定义变量\n定义方式\n```javascript\n    injector.value('configInfo',{\n        info:'configInfo'\n    });\n    var configInfo = injector.getValue('configInfo');\n```\n### 定义Service：\n```javascript\n    function serviceA(){\n        this.serviceText = 'serviceA hello world' + Math.random();\n        this.getText = function(){\n            return this.getText();\n        }\n    }\n```\n#### 定义方式：\n1.通过类型获取Service\n```javascript\n    injector.service(serviceA);\n    var sA = injector.getService(serviceA);\n    sA.getText(); // 'serviceA hello world..'\n```\n2.通过名称获取Service\n```javascript\n    injector.service('serviceA',serviceA);\n    var sA = injector.getService(serviceA) 或者 injector.getService('serviceA')\n    sA.getText(); // 'serviceA hello world..'\n```\n3.Service是单例的\n```javascript\n   var sB =  injector.getService(serviceA);\n   sB === sA; //true\n   \n   //获取不同的实例\n   var sC = injector.getFactory(serviceA) 或 injector.getFactory('serviceA');\n   sC === sB // false\n```\n4.Service的名称\n```javascript\n    //定义Service的时候不指定Service名称,injector会自动生成默认的Service名称\n     injector.service(serviceA);\n     Injector.identify(serviceA); //获取Service名称\n     Injector.identify(serviceA,'serviceA_name'); //自定义Service的名称\n     //获取Service\n     var sA = injector.getService('serviceA_name'); //通过名称获取\n     sA = injector.getService(serviceA); //通过类型获取\n     \n    //注:在声明Service的时候定义名称,这个名称拥有最高优先级,会覆盖之前定义的Service名称\n    injector.service('serviceA_name_new',serviceA);\n    sA = injector.getService('serviceA_name_new');\n```\n5.Service的依赖注入\n```javascript\n    function serviceB(serviceA){\n        this.getText = function(){\n            return serviceA.getText();\n        }\n    }\n    injector.service('serviceB',serviceB);\n    var sB = injector.getService('serviceB');\n    sB.getText(); // 'serviceA hello world ..'\n    //注：这种方式仅在代码为经过压缩的时候生效，且serviceA的名称是serviceA时可用\n    \n    //定义依赖\n    serviceB.$injector = [serviceA]; //通过类型注入\n    serviceB.$injector = ['serviceA'];//通过名称注入\n    \n    //声明时定义依赖\n    injector.service('serviceB',['serviceA',serviceB]); //依赖通过名称注入\n    injector.service('serviceB',[serviceA,serviceB]); //依赖通过类型注入\n    \n    注:Service依赖可以时已经声明的Value、Service、Factory,如依赖的是Service，则会返回单例的实例\n```\n### 定义Factory\n```javascript\n    function factoryA(){\n        return {\n            text:'factoryA helloworld' + Math.random(),\n            getText:function(){\n                return this.text;\n            }\n        };\n    }\n```\n#### 定义方式\n1.通过类型\n```javascript\n    injector.factory(factoryA);\n    var fA = injector.getFactory(factory);\n    fA.getText(); // 'factoryA helloworld..'\n    \n    var fA_ = injector.getFactory(factory);\n    fA === fA_; // false\n    \n    var fA__ = injector.getService(factory);\n    fA__.getText(); // 'factoryA helloworld..' 与getFactory 等效\n```\n2.通过名称\n```javascript\n    injector.factory('factoryA',factoryA);\n    var fA = injector.getFactory('factoryA') 或 injector.getFactory(factoryA);\n    fA.getText(); // 'factoryA helloworld..'\n```\n3.Factory名称\n```javascript\n//定义Factory的时候不指定Factory名称,injector会自动生成默认的Service名称\n     injector.factory(factoryA);\n     Injector.identify(factoryA); //获取Factory名称\n     Injector.identify(factoryA,'factoryA_name'); //自定义Factory的名称\n     //获取Service\n     var fA = injector.getFactory('factoryA_name'); //通过名称获取\n     fA = injector.getFactory(factoryA); //通过类型获取\n     \n    //注:在声明Factory的时候定义名称,这个名称拥有最高优先级,会覆盖之前定义的Factory名称\n    injector.factory('factoryA_name_new',factoryA);\n    fA = injector.getFactory('factoryA_name_new');\n```\n4.factory名称\n```javascript\n    function factoryB(factoryA){\n        this.getText = function(){\n            return factoryA.getText();\n        }\n    }\n    injector.factory('factoryB',factoryB);\n    var fB = injector.getFactory('factoryB');\n    fB.getText(); // 'factoryA helloworld..'\n    //注：这种方式仅在代码为经过压缩的时候生效，且factoryA的名称是factoryA时可用\n    \n    //定义依赖\n    factoryB.$injector = [factoryA]; //通过类型注入\n    factoryB.$injector = ['factoryA'];//通过名称注入\n    \n    //声明时定义依赖\n    injector.factory('factoryB',['factoryA',factoryB]); //依赖通过名称注入\n    injector.factory('factoryB',[factoryA,factoryB]); //依赖通过类型注入\n    \n    注:Service依赖可以时已经声明的Value、Service、Factory,如依赖的是Service，则会返回新创建的实例\n```\n### 调用函数\n```javascript\n    injector.invoke(function(){\n        console.log('invoke function');\n    });\n    injector.invoke(['serviceA',function(sA){\n        sA.getText();\n    }]);\n    injector.invoke([serviceA,function(sA){\n        sA.getText();\n    }]);\n```\n### 定义Provider\n1.定义方式\n```javascript\n    function providerA(){\n        this.testText = 'providerA text';\n        this.$get = function(){\n            return this.testText;\n        }\n    }\n    injector.provider(providerA);\n    var pA = injector.getProvider(providerA);\n    pA.$get(); // 'providerA text';\n    var pA_ = injector.getProvider(providerA);\n    pA === pA_; //true Provider是单例的\n    \n    注：Provider的定义与Service、Factory类似，区别在于Provider只能依赖已经声明的Provider,且必须包含一个$get方法签名\n```\n### Injector的继承\n1.继承方式\n```javascript\n    var injectorB = new HERE.Injector(injector);\n    var sA = injectorB.getService('serviceA'); //获取injector中定义的Service\n    injectorB.service('serviceA',function(){\n        this.getText = function(){\n            return 'serviceB from injectorB';\n        };\n    });\n    sA = injectorB.getService('serviceA');\n    sA.getText(); // 'serviceB from injectorB'\n```\n2.多继承\n```javascript\n    var injectorC = new HERE.Injector();\n    var injectorD = new HERE.Injector(injectorA,injectorC); //或 new HERE.Injector([injectorA,injectorC]);\n    injectorD.getService('serviceA');\n    //注:获取Service、Factory、Provider、Value顺序：先从injectorD中获取，获取不到则从injectorA、injectorC中获取\n```\n\n### 配置\n```javascript\n    Injector.config({\n        debugMode:true,\n        injectorIdentifyKey:'$injectorName',\n        injectorDepIdentifyKey:'$injector'\n    });\n    //注:配置需要在初始化Injector之前进行，初始化后不能进行配置，一般不需要配置，当$injectorName、$injector发生命名冲突时才需要配置\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkouyjes%2Finjector-ioc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkouyjes%2Finjector-ioc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkouyjes%2Finjector-ioc/lists"}