我的Angular 组件往往有一个全局状态(或"模式"),所以我正在寻找一种有效的编码方法.我想做的是:
@Component({
....
})
export class AbcComponent implements OnInit {
enum State {
init, view, edit, create, wait
}
state: State = State.init;
其思想是,只需设置state属性,AbcComponent中的函数就可以驱动模板的操作.例如:
<div class="col" *ngIf="state === State.view"> ... </div>
问题是enum定义不能出现在class struct 中.然后如果我把它移到class struct 之外,那么模板就不在它的局部范围内.
有不同的方法吗?
另外,如果有任何兴趣的话,我一直在做的是,我有boolean个属性,每个州一个.例如modeInit modeView等.它可以工作,但很笨拙,因为一次只能有一个是true.