2017-07-14 24 views
1

在一個部件,我可以通過導入它使用OnInit如何隱藏Angular 2功能模塊中組件的位置?

import { OnInit } from '@angular/core'; 

我並不需要知道OnInit源的確切位置。 Angular/core有一種隱藏確切位置的方法。

我想創建一個包含接口「MyInterface的」功能模塊「一般」,並能夠在其他模塊中使用它通過導入它像

import { MyInterface } from 'general'; 

我應該怎麼做隱藏確切MyInterface的位置?一些索引文件?

+0

聽起來像是你正在尋找https://stackoverflow.com/questions/37564906/what-are-all-the-index-ts-used-for –

回答

0

您需要從常規中導出類/函數,以便將它們導入到另一個文件中。

  1. index.tsgeneral.ts新建文件夾 「富」

index.ts

export * from './general.ts'; 
//export * from './subfolder/otherfiles.ts'; 

general.ts

interface A { 
    myRequiredMethod(): void; 

} 

class B { 
    anotherVariable: number = 2; 

} 

export { A, B }; 

myAwesomeFile.ts

import { A, B } from './foo; 
class MyAwesomeFile implements A { 
constructor() { 
    const foo = new B(); 
    console.log(foo.anotherVariable); 

} 
myRequiredMethod() { 
    //implements myRequiredMethod from the "A" interface in general.ts 
} 
} 
+0

謝謝爲您的貢獻LT56!但問題不在於只導入一個組件。 'General'是一個NgModule。它從NgModule導入'MyInterface'的目標,而不需要指定該NgModule中的源文件。類似於我們可以從@ angular/core中導入OnInit的方式,而無需知道OnInit在模塊「內核」中的位置。 –

+0

我更新了上面的代碼示例。您正在從@ angular/core導入界面「OnInit」。該接口包含ngOnInit()方法,該方法將由實現「OnInit」接口的任何類實現。 – LT56