两个方法都有其独特的优点和适用场景,具体看具体情况。以下是解释:
示例代码:
// Container Component import { Component, OnInit } from '@angular/core'; import { UserService } from '../user.service';
@Component({
selector: 'app-user-list-container',
template: '
constructor(private userService: UserService) {}
ngOnInit() { this.users = this.userService.getUsers(); }
onUserSelected(user: User) { console.log('Selected User:', user); } }
// Presentational Component import { Component, Input, Output, EventEmitter } from '@angular/core'; import { User } from '../user';
@Component({
selector: 'app-user-list',
template:
,
})
export class UserListComponent {
@Input() users: User[];
@Output() userSelected = new EventEmitter
下一篇:按公共嵌套值对分组嵌套字典