在角2依赖注入与ES5 [英] Dependency Injection in Angular 2 with ES5
问题描述
假如我有两个单独的组件
。其中定义了我的应用
元素,一是定义了孩子
元素。
我要的孩子
组件是我的应用
(根)组件的依赖关系。
这是如何做呢?
myapp.component.js
(功能(应用程序){
app.AppComponent = ng.core
。零件({
选择:我的应用,
模板:'< H1>我的第一角2应用!!< / H1>'
})
。类({
构造:函数(){}
});
})(window.app ||(window.app = {}));
child.component.js
(功能(应用程序){
app.ChildComponent = ng.core
。零件({
选择:'孩子',
模板:'&所述; H3>这是子< / H3>', })
。类({
构造:函数(){}
});
})(window.app ||(window.app = {}));
在事实上,它是类似于我们在打字稿版本;-)东西。您需要配置供应商:
- 在
引导
方法中应用程序级 - 在
提供商内的组件级
属性
有两个伟大的博客文章写作与ES5 Angular2应用程序时,可以帮助你:
- http://gurustop.net/blog/2015/ 12/16 / angular2-β-的JavaScript组件通过Meligy
- HTTP ://blog.thoughtram.io/angular/2015/05/09/writing-angular-2-$c$c-in-es5.html 通过帕斯卡尔precht
- 的http://blog.thoughtram.io/angular/2015/07/06/even-better-es5-$c$c-for-angular-2.html由Pascal precht
下面是ES5一个完整的工作只样本:
-
index.html的作为JavaScript文件包括
<!DOCTYPE HTML>
< HTML和GT;
< HEAD>
<脚本SRC =HTTPS://$c$c.angularjs.org/2.0.0-beta.0/Rx.umd.js>< / SCRIPT>
<脚本SRC =HTTPS://$c$c.angularjs.org/2.0.0-beta.0/angular2-polyfills.js>< / SCRIPT>
<脚本SRC =HTTPS://$c$c.angularjs.org/2.0.0-beta.0/angular2-all.umd.dev.js>< / SCRIPT>
&所述; SCRIPT SRC =main.js>&下; /脚本>
< /头>
<身体GT;
< CMP>&放大器; LT; / CMP>
< /身体GT;
< / HTML> -
该服务的定义
VAR服务=功能(){};
Service.prototype.greeting =功能(){
返回'你好';
}; -
组件的定义
VAR CMP =
ng.core.Component({
选择:CMP
})。
视图({
模板:{{问候}}的世界!
})。
类({
构造函数:[服务功能(服务){
this.greeting = service.greeting();
}]
}); -
引导的
CMP
组件为应用程序组件document.addEventListener('DOMContentLoaded',函数(){
ng.platform.browser.bootstrap(CMP);
}); -
依赖注入在应用程序级。只要引导函数添加第二个参数
。它的值对应于包含
服务
对象的数组。document.addEventListener('DOMContentLoaded',函数(){
ng.platform.browser.bootstrap(CMP,[服务]);
}); -
依赖注入组件级。只需添加一个
提供商
属性组件配置对象。它的值是一个包含服务数组
对象。VAR CMP = ng.core。
零件({
选择:CMP,
供应商:[服务]
})。
视图({
模板:{{问候}}的世界!
})。
类({
构造函数:[服务功能(服务){
this.greeting = service.greeting();
}]
});
如果您要使用另一个里面一个组件,只需利用指令
属性,如下所述。在 CmpChild
组件在 CMP
人用。
VAR CmpChild = ng.core。
零件({
选择:'孩子'
})。
视图({
模板:{{问候}}的世界!
})。
类({
构造函数:[服务功能(服务){
this.greeting = service.greeting();
}]
});VAR CMP = ng.core。
零件({
选择:CMP
})。
视图({
模板:'<儿童>< /儿童>,
指令:[CmpChild]
})。
类({
构造函数:[服务功能(服务){ }]
});
希望它可以帮助你,
蒂埃里
Suppose I've got two separate components
. One defines the my-app
element, one defines the child
element.
I want the child
component to be a dependency of the my-app
(root) component.
How is this done?
myapp.component.js
(function(app) {
app.AppComponent = ng.core
.Component({
selector: 'my-app',
template: '<h1>My First Angular 2 App!!</h1>'
})
.Class({
constructor: function() {}
});
})(window.app || (window.app = {}));
child.component.js
(function(app) {
app.ChildComponent = ng.core
.Component({
selector: 'child',
template: '<h3>This is the child</h3>',
})
.Class({
constructor: function() {}
});
})(window.app || (window.app = {}));
In fact, it's something similar to what we have in the Typescript version ;-). You need to configure providers:
- At the application-level within the
bootstrap
method - At the component-level within the
providers
property
There are two great blog posts that can help you when writing Angular2 applications with ES5:
- http://gurustop.net/blog/2015/12/16/angular2-beta-javascript-component by Meligy
- http://blog.thoughtram.io/angular/2015/05/09/writing-angular-2-code-in-es5.html by Pascal Precht
- http://blog.thoughtram.io/angular/2015/07/06/even-better-es5-code-for-angular-2.html by Pascal Precht
Here is a complete working sample with ES5 only:
index.html for JavaScript files to include
<!DOCTYPE html> <html> <head> <script src="https://code.angularjs.org/2.0.0-beta.0/Rx.umd.js"></script> <script src="https://code.angularjs.org/2.0.0-beta.0/angular2-polyfills.js"></script> <script src="https://code.angularjs.org/2.0.0-beta.0/angular2-all.umd.dev.js"></script> <script src="main.js"></script> </head> <body> <cmp></cmp> </body> </html>
Definition of the service
var Service = function() {}; Service.prototype.greeting = function() { return 'hello'; };
Definition of the component
var Cmp = ng.core.Component({ selector: 'cmp' }). View({ template: '{{greeting}} world!' }). Class({ constructor: [Service, function(service) { this.greeting = service.greeting(); }] });
Bootstrap the
Cmp
component as application componentdocument.addEventListener('DOMContentLoaded', function() { ng.platform.browser.bootstrap(Cmp); });
Dependency injection at application level. Simply add a second parameter to the
bootstrap
function. Its value corresponds to an array containing theService
object.document.addEventListener('DOMContentLoaded', function() { ng.platform.browser.bootstrap(Cmp, [Service]); });
Dependency injection at component level. Simply add a
providers
property in the component configuration object. Its value is an array containing theService
object.var Cmp = ng.core. Component({ selector: 'cmp', providers: [Service] }). View({ template: '{{greeting}} world!' }). Class({ constructor: [Service, function(service) { this.greeting = service.greeting(); }] });
If you want to use a component inside another one, simply leverage the directives
attribute of the view, as described below. The CmpChild
component is used within the Cmp
one.
var CmpChild = ng.core.
Component({
selector: 'child'
}).
View({
template: '{{greeting}} world!'
}).
Class({
constructor: [Service, function(service) {
this.greeting = service.greeting();
}]
});
var Cmp = ng.core.
Component({
selector: 'cmp'
}).
View({
template: '<child></child>',
directives: [CmpChild]
}).
Class({
constructor: [Service, function(service) {
}]
});
Hope it helps you, Thierry
这篇关于在角2依赖注入与ES5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!