我在ES5中玩Angular 2。 我意识到 ES5 不是最新和最好的,但由于项目要求和对我可以使用的内容的限制,我必须使用它。
我创建了一个组件,并尝试在其中使用另一个组件作为指令。 Angular 似乎无法识别嵌套组件。 我在渲染的 html 源代码中看到它。 但是什么都没有渲染。 我觉得我错过了一个基本概念。
我创建了一个 Plunker 来玩代码。
(function() {
var helloComponenet = ng.core.Component({
selector: 'hello-comp',
template: '<h2> Hello Component </h2>'
})
.Class({
constructor: function() {}
});
var myApp = ng.core.Component({
selector: 'my-app',
template: '<h1> Does Something </h1> <br>' + '<hello-comp></hello-comp>'
})
.Class({
constructor: function() {},
directives: [helloComponenet]
});
document.addEventListener('DOMContentLoaded', function() {
ng.platform.browser.bootstrap(myApp);
});
}());
我基于 https://www.gurustop.net/blog/2015/12/16/angular2-beta-javascript-component 以及其他一些示例来构建上述示例。 我没有意识到上面的例子是一年前的。
同样在上面的代码中,指令属性应该在组件中,而不是类属性中。