【发布时间】:2016-04-21 11:17:02
【问题描述】:
我正在尝试学习 Angular 2,所以我制作了一些 hello world 示例。
这是我的代码:
boot.ts
import {bootstrap} from 'angular2/platform/browser'
import {AppComponent} from './app.component'
import {DataService} from './app.dataservice'
bootstrap(AppComponent, [DataService]);
index.html
...
<body>
<hello-world>Loading...</hello-world>
<hello-world>Loading...</hello-world>
</body>
...
app.component.ts
import {Component} from 'angular2/core';
import {DataService} from './app.dataservice'
@Component({
selector: 'hello-world',
template: '<h1>Hello {{ item }}</h1>'
})
export class AppComponent {
items: Array<number>;
item: number;
constructor(dataService: DataService) {
this.items = dataService.getItems();
this.item = this.items[0];
}
}
app.dataservice.ts
export class DataService {
items: Array<number>;
constructor() {
this.items = [1,2,3];
}
getItems() {
return this.items;
}
}
代码似乎可以正常工作,因为第一个 hello-world 自定义标记与 ts 中的代码一起正确显示。但是,第二个hello-world tag is not transformed。仅显示一个自定义元素。
不能超过 1 个自定义标签?我该怎么做?
编辑
我在 app.components.ts 中添加了新的导入
import {ByeWorld} from './app.byeworld';
在 app.byeworld.ts
中import {Component} from 'angular2/core';
@Component({
selector: 'bye-world',
template: '<h1>Bye World</h1>'
})
export class ByeWorld {
constructor() {
}
}
【问题讨论】:
-
是的,但我想插入相同的自定义标签,两次
-
这是你的主要组件吗?
-
@ShaohaoLin 我也遇到了与
2.0.0-beta.1版本相同的问题.. 看起来很奇怪。它在页面上一次引导应用程序..其他元素标签被忽略.. -
我猜
bootstrap()只会引导它找到的第一个实例。也就是说,你不能在同一个应用程序中有两个根组件,这是有道理的,因为 Angular 构建了一个组件树,而树不能有两个根。 -
@Pablo 我认为我们的应用程序中可以有多个
main-component,但它们应该有不同的名称......就像Angular1有能力有多个应用程序(规则是根组件不应该嵌套在彼此)
标签: javascript typescript angular