2016-09-23 54 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