2016-09-23 33 views
0

我不认为这是可能的,但考虑到这一点:访问的关键

接口

import {BrowserService} from "../services/browser/index"; 

export interface IPrimaryNavigation { 
    opts:IPrimaryNavigationOpts; 
} 

export interface IPrimaryNavigationOpts { 
    ... 
    browserService:BrowserService; 
    ... 
} 

类:

import {IPrimaryNavigation, IPrimaryNavigationOpts} from "./interface"; 

export class PrimaryNavigation implements IPrimaryNavigation { 
    public opts:IPrimaryNavigationOpts; 
    ... 
    mounted():void { 
     ... 
     this.listenForBootstrap(this.opts.bsNav, this.opts.browserService); 
    } 
    listenForBootstrap(menuName:string,browserService:<???>):void { 
                 ^^^ here is the problem - 
    // I want to do the equivalent of IPrimaryNavigationOpts.browserService but can't. 
    // I don't think I should have to import the IBrowserService explicitly. 

    } 
} 

怎么办你解决了这个问题。我似乎无法在网上找到任何涉及此类问题的示例。我承认我对这一切都很新颖,所以赞赏。

回答

1

我们可以再出口使用的东西。因此,“./interface”里面我们可以做(检查最后几行)

import {BrowserService} from "../services/browser/index"; 

export interface IPrimaryNavigation { 
    opts:IPrimaryNavigationOpts; 
} 

export interface IPrimaryNavigationOpts { 
    ... 
    browserService:BrowserService; 
    ... 
} 
// re-export used interface 
export { BrowserService } 

而现在,我们可以导入甚至型

// we import the re-exported type 
import {IPrimaryNavigation, IPrimaryNavigationOpts 
     BrowserService } from "./interface"; 

export class PrimaryNavigation implements IPrimaryNavigation { 
    public opts:IPrimaryNavigationOpts; 
    ... 
    mounted():void { 
     ... 
     this.listenForBootstrap(this.opts.bsNav, this.opts.browserService); 
    } 
    listenForBootstrap(menuName:string,browserService:BrowserService):void { 
    //listenForBootstrap(menuName:string,browserService:<???>):void { 
    //             ^^^ here is the problem - 
    // I want to do the equivalent of IPrimaryNavigationOpts.browserService but can't. 
    // I don't think I should have to import the IBrowserService explicitly. 

    } 
} 
+0

其实,这是正确的答案。当我尝试@ AlexG使用typeof的方法时,Typescript仍然抱怨说它找不到'IPrimaryNavigationOpts'接口 – Simon

0

您可以键入如下:browserService: typeof IPrimaryNavigationOpts.browserService

+0

'错误TS2304:找不到名字'IPrimaryNavigationOpts''这很奇怪。 – Simon