2

We are developing a new App in Angular2 with typescript. Is there any cleaner way to declare all the classes and interfaces in separate files and referencing them in angular2 components .

import {Component, OnInit, Pipe} from 'node_modules/angular2/core';
import { HTTP_PROVIDERS, Http, Response, Headers } from 'node_modules/angular2/http';
import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, Router } from 'node_modules/angular2/router';
import { Observable } from 'node_modules/rxjs/Rx';

@Component({
  selector: 'my-App',
  templateUrl: '/app/Index.html',
  directives: [ROUTER_DIRECTIVES, NewEvent]   })
//instead of writing class here , is there any way to declare this class in a
//separate file and injecting it here. 
export class Events    {    }
1
  • 2
    All your imports at the top of the line import classes from other files. You can do the same. See also plnkr.co/edit/?p=preview Commented Jul 18, 2016 at 15:38

1 Answer 1

1

You need to declare the class of components within the same module than decorators applied on them.

@Component({ // Applied decorator
  (...)
})
export class Events { // Component class
}

Decorators and classes can be imported from other modules:

import {Component} from '@angular/core'; // decorator from Angular2

import {SomeOtherClass} from './some-module'; // custom class
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.