2017-09-04 91 views
0

Example.ts無法定位spec文件

export class Example{ 

public async initService(Id): Promise<any> { 

//promise logic 

    } 
} 

Example.spec.ts

//imported Example class correctly 

    describe('testing', async() =>{ 
     it('InitService test call', async()=>{ 
      let x = await Example.initService(id:any) //this line displays error as initService does not exist on type 'typeof AnnounceService' 
    }); 
}); 

我已經正確導入的類的功能,但隨後也無法調用的功能示例類中的示例.spec.ts

+0

是'Example'是服務還是組件?你是否靜態調用? –

回答

0

這是一個非常簡單的錯誤。 您正在調用類函數本身的方法,而不是該類實例類型的對象。

如果您打算調用的方法沒有一個實例,當你做,你的示例代碼,那麼你需要將其標記爲靜態:

export class Example { 
    static async initService(Id) {} 
} 

如果,另一方面,你實際上意味着它是你需要以代替創建一個實例調用該方法的實例方法:

export class Example { 
    async initService(Id) {} 
} 

describe('testing', async() => { 
    it('InitService test call', async() => { 
     const x = await new Example().initService(1); 
    }); 
}); 

最後,值得注意的是,以這樣的方式表述錯誤文本ID是因爲類的類型功能本身是寫爲typeof ClassFunction,而它的實例類型只寫成`ClassFunction

相關問題