我目前有以下模板
<project-form [nextId]="projects.length" (newProject)="addProject($event)"></project-form>
<project-list [projects]="projects"></project-list>
在 ProjectAppComponent 中。
class ProjectAppComponent {
projects: Project[] = [
{ id: 0, title: "Build the issue tracker" },
{ id: 1, title: "Basecamp" },
]
addProject(project: Project) {
this.projects.push(project);
}
}
ProjectAppComponent 具有项目数组和将新项目推入其中的方法。我想为项目表单和项目列表创建子路由,以便我可以执行/projects/new
并/projects/show
显示表单或列表。我创建了这样的路线配置 -
@Component({
template: `
<div>
<router-outlet></router-outlet>
</div>
`,
directives: [ RouterOutlet ]
})
@RouteConfig([
{ path: '/list', name: 'ProjectList', component: ProjectListComponent, useAsDefault: true },
{ path: '/new', name: 'ProjectForm', component: ProjectFormComponent },
])
class ProjectAppComponent {
projects: Project[] = [
{ id: 0, title: "Build the issue tracker" },
{ id: 1, title: "Basecamp" },
]
addProject(project: Project) {
this.projects.push(project);
}
}
对于 ProjectAppComponent 类本身。现在的问题是我不知道如何将项目数组([projects]="projects"
在模板中)传递给 ProjectListComponent,因为<project-list>
不再使用选择器(必须使用<router-outlet>
)。ProjectListComponent 依赖于@Input() project: Project
渲染所有项目。我应该如何解决这个问题?这是项目列表组件 -
@Component({
selector: 'project-list',
template: `
<ul>
<project-component *ngFor="#project of projects" [project]="project"></project-component>
</ul>
`,
directives: [ProjectComponent]
})
class ProjectListComponent {
@Input() projects: Project[];
}