2016-02-24 37 views
1

下面的代码失败,出现错误:打字稿接口无法工作

readCandidates is not a function:

下面是代码

export class Candidates {  
    private _dataService : ModelContracts.IDataService;  

    constructor(private tag: ModelContracts.ITag, private dataService: ModelContracts.IDataService) { 
     this._value = tag; 
     this._dataService = dataService;   
    } 

    private _value : ModelContracts.ITag; 
    public get value() : ModelContracts.ITag { 
     return this._value; 
    } 
    public set value(v : ModelContracts.ITag) { 
     this._value = v; 
    } 

    candidates = []; 

    activate() {   
     this._dataService.readCandidates().then(candidates => this.candidates = candidates); 
    }  
} 

export interface IDataService { 
    readCandidates(): Promise<ModelContracts.ICandidate[]> 
} 

export class DataService { 
    //some implementation 
} 

我使用Aurelia路上打赌1.1.0和打字稿。 dataService依赖项被注入,但函数调用失败。

+2

在您提供的代码中没有对'getCandidates'的调用。问题必须在代码的其他部分。 – zlumer

+0

是的,这是一个错字。我的意思是'readCanidates'... –

+0

没有办法告诉问题是什么,如果dataService是一个有效的IDataService,它将有一个'readCandidates()'函数,并且你的应用程序可以工作。 –

回答

1

如果调用的上下文不是类(例如,因为您打电话给activate作为回调或事件),您需要确保您清理范围。

例如:

activate =() => {   
    this._dataService.readCandidates().then(candidates => this.candidates = candidates); 
} 

虽然有一些更好的方法,如果你想拥有a consitent approach to handling the responsibility of scope in TypeScript做到这一点。这个建议是要处理课外活动,因为课程不应该知道它将如何被调用。

+0

感谢您的回应。这并不能解决问题。 –

+0

@GenaVerdel请问您能否提供一些有关该方法被调用的更多细节? – Fenton

+0

感谢您的回复。看来我真的可以在aurelia中使用依赖注入的接口。 –

0

在aurelia中使用DI接口是不可能的。