我有一个接收字符串数组的组件,但在内部我需要有一个Foo数组:
class Foo {
constructor( name: string ) { }
}
我试着这样做:
@Component( {
selector: 'my-app-boo',
templateUrl: './boo.component.html'
} )
export class BooComponent implements OnInit {
private _columns: Foo[];
@Input()
set columns( columns: string[] ) {
this._columns = columns.map( column => new Foo( column ) );
}
get columns(): Foo[] {
return this._columns;
}
}
但是我不能这样做,因为getter和setter必须属于同一类型。
我怎样才能优雅地做到这一点?
答案 0 :(得分:0)
试试这个:
class Foo {
constructor( name: string ) { }
}
@Component( {
selector: 'my-app-boo',
templateUrl: './boo.component.html'
} )
export class BooComponent implements OnInit {
private _columns: Foo[];
@Input()
set columns(columns: Foo[] ) {
this._columns = columns.map( column => column);
}
get columns(): Foo[] {
return this._columns;
}
}
答案 1 :(得分:0)
这就是我最终做到的方式:
@Component( {
selector: 'my-app-boo',
templateUrl: './boo.component.html'
} )
export class BooComponent implements OnInit {
private _columns: string[];
private columnOptions: Foo[];
@Input( 'columns' )
set columns( columns: string[] ) {
this._columns = columns;
this.columnOptions = columns.map( column => new Foo( column ) );
}
}