【发布时间】:2017-09-08 14:50:32
【问题描述】:
在这里转圈。对 Typescript 来说非常新,它在琐碎的实现中引起了很大的麻烦。
如何在 AgentStatusService 中定义它应该有一个名为 ['offline','available','busy','away'] 的 4 个选项的数组? AgentStatus 已定义(或者是?),我将其注入AgentStatusService。
Microsoft Visual Studio Code 在第 21 行出现问题,其中类型 'typeof AgentStatus' 不能分配给类型 'AgentStatus'...为什么?
更新:
import { EventEmitter, Injectable } from '@angular/core';
export enum AgentStatus {
available =1 ,
busy = 2,
away = 3,
offline = 0
}
export interface IAgentStatusService {
state: number
states: AgentStatus
}
@Injectable()
export class AgentStatusService implements IAgentStatusService {
state:number; // this really should be string, but line 22 returns a number
states:AgentStatus;
constructor(states:typeof AgentStatus = AgentStatus){
// unreacheable code browser_adapter.ts:78EXCEPTION: Error: Uncaught (in promise): TypeError: Cannot read property 'isSkipSelf' of null
// absolutely impossible to debug...
this.state = AgentStatus.offline // this returns a number
}
// set state(state:string){
// try{
// this._state = this.states[state];
// // string
// } catch(e){
// console.log('tried setting bad enum value on AgentStatus', e.stack);
// }
// }
// get state():string{
// return this._state;
// }
// get model():any {
// return this.states;
// }
}
比较 这个实现满足angular2:
@Injectable()
export class AgentStatusService {
public states = ['offline','available','busy','away'];
private _state;
constructor(){
this._state = this.states[0];
}
set state(state:string){
try{
this._state = this.states[state];
} catch(e){
console.log('tried setting bad enum value on AgentStatus', e.stack);
}
}
get state():string{
return this._state;
}
get model():any {
return this.states;
}
}
【问题讨论】:
标签: typescript angular angular2-services angular2-forms