CorayThan CorayThan - 23 days ago 7
TypeScript Question

How to export typescript enum with systemjs and Angular2?

I'm trying to use Angular2 with system.js and typescript. It's been working fine for the interfaces I've been exporting so far, but as soon as I try to import an enum it all blows up. In the following code,

Card
works fine, but when I import
Legend
I get the error
GET http://localhost:3000/shared/legend 404 (Not Found)
.

import {Component, OnInit} from 'angular2/core';
import {CardService} from './card.service';
import {Card} from '../../shared/card';
import {Legend} from '../../shared/legend';

@Component({
selector: 'cards-list',
templateUrl: 'app/cards-list.html',
providers: [CardService]
})
export class CardsListComponent implements OnInit {
public cards:Card[];

//using Legend is the problem!
public legendValues:String[] = [Legend[Legend.ARIANE]];

constructor(private _cardService:CardService) {
}
getCards() {
this._cardService.getCards(cards => this.cards = cards);
}
ngOnInit() {
this.getCards();
}
}


Here is the legend file:

export enum Legend {
THE_RAPTOR,
LINZA,
ARIANE,
OZAN
}


Why can't I import a normal enum?

Answer

It's been working fine for the interfaces I've been exporting so far, but as soon as I try to import an enum it all blows up

Interfaces are a compile time only construct and therefore have no runtime impact. The files with the interfaces are not loaded at runtime. However files with runtime stuff like classes/variables/enums etc. are loaded at runtime and you are getting a 404 on these. This is a server setup error. You need to allow loading these JS files.

Comments