我正在学习独立组件的Angular 17,但我无法使用属性 Select 器来处理我的组件.
如文档中所解释的,我应该能够为我的组件https://angular.dev/guide/components/selectors#types-of-selectors使用属性
当我try 它时,我得到以下错误
无法绑定到‘app-list-Item’,因为它不是‘tr’的已知属性.
我试着制作了一个类型和类 Select 器,它们的工作情况与预期一致. 使用属性 Select 器时.我收到一个错误.
这是我的Angular 组件. 可以在这里找到问题的堆积如山:https://stackblitz.com/edit/angular-17-starter-project-4t9qcr?file=src%2Flist.component.ts
list-item-component.ts个
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-list-item, .app-list-item, [app-list-item]',
standalone: true,
imports: [],
template: `
<td>{{ item.A }}</td>
<td>{{ item.B }}</td>
`,
styles: ``,
})
export class ListItemComponent {
@Input() item!: { A: string; B: string };
}
list.component.ts个
import { Component } from '@angular/core';
import { ListItemComponent } from './list-item.component';
@Component({
selector: 'app-list',
standalone: true,
template: `
<div>
<h3>Type selector</h3>
<table>
<tr>
<th>A</th>
<th>B</th>
</tr>
@for (item of data; track $index){
<tr>
<app-list-item [item]="item"/>
</tr>
}
</table>
<h3>class selector</h3>
<table>
<tr>
<th>A</th>
<th>B</th>
</tr>
@for (item of data; track $index){
<tr class="app-list-item" [item]="item"></tr>
}
</table>
<h3>Attribute selector</h3>
<table>
<tr>
<th>A</th>
<th>B</th>
</tr>
@for (item of data; track $index){
<tr [app-list-item] [item]="item"></tr>
}
</table>
</div>
`,
styles: ``,
imports: [ListItemComponent],
})
export class ListComponent {
data = [
{ A: 'a1', B: 'b1' },
{ A: 'a2', B: 'b2' },
];
}
当您删除<tr [app-list-item] [item]="item"></tr>
时,它会按预期工作,但我更喜欢有属性的解决方案.