我有一个从示例 API 获取数据的简单应用程序。在将指令定义为组件时试图找出问题所在:
app.ts
import {Component, View, bootstrap, For} from 'angular2/angular2';
import {ArticlesSvc} from 'services/articlesSvc';
import {ArticleItem} from 'directives/ArticleItem';
@Component({
selector: 'main-app',
injectables: [ArticlesSvc]
})
@View({
template: `<p *for="#post of posts">{{ post.title }} <article-item [body]="post.body"></article-item></p>`,
directives: [For,ArticleItem]
})
class MainComponent {
posts: Array<string>;
constructor(articlesSvc: ArticlesSvc) {
this.posts = [];
articlesSvc.get('http://jsonplaceholder.typicode.com/posts').then((data) => {
this.posts = data;
});
}
}
bootstrap(MainComponent);
这是ArticleItem
组件:
import {Component, View} from 'angular2/angular2';
@Component({
selector: 'article-item',
properties: ['body']
})
@View({
template: `<p>{{ body }}</p>`
})
export class ArticleItem {
body:string;
}
出于某种原因,它给了我Unexpected number
错误。如何正确连接这两个组件?它是用自己的视图定义子组件的正确方法吗?