1

In a component I can use OnInit by importing it:

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

I don't need to know the exact location of the OnInit source. Angular/core has a way to hide the exact location.

I want to create a feature-module 'general' that contains interface 'MyInterface', and be able to use it in other modules by importing it like

import { MyInterface } from 'general';

What should I do to hide the exact location of MyInterface? Some index-file?

The Hungry Dictator
  • 3,444
  • 5
  • 37
  • 53
M. Hobijn
  • 11
  • 2

1 Answers1

0

You need to export the class/function from general in order to import them in another file.

  1. Create folder "foo" with index.ts and general.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
 }
}
LT56
  • 187
  • 6
  • Thanks for your contribution LT56! But the problem is not to import just a component. 'General' is an NgModule. The goal it to import 'MyInterface' from the NgModule, without the need to specifiy the sourcefile within that NgModule. Similar to the way we can import OnInit from @angular/core without knowing where OnInit is within module 'core'. – M. Hobijn Jul 16 '17 at 14:42
  • I updated the code example above. You are importing the interface "OnInit" from @angular/core. That interface contains the method ngOnInit() which will be required to implement by any class that implements the "OnInit" interface. – LT56 Jul 16 '17 at 14:56