2017-09-04 170 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