CorayThan
CorayThan

Reputation: 17825

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?

Upvotes: 3

Views: 2685

Answers (3)

BERGUIGA Mohamed Amine
BERGUIGA Mohamed Amine

Reputation: 6290

In the bellow code I'll demonstrate how to EXPORT class (constClass) contain list of const that can be used in your class service.

-serviceAngularClass.js-

    ...
    import {WsRessources} from "./constClass";

            @Injectable()
            export class PersonneService {...

        public functionName():Observable<any>{
            return this.updateRessource(WsRessources.BasePath+WsRessources.affectLeavingDate...);


        }
}//end Class  

-constClass.js-

    export class WsRessources {
        public static BasePath='personnes';
        public static affectLeavingDate='/affectLeavingDate';
     }

Upvotes: 0

Michael Shaw
Michael Shaw

Reputation: 153

I had a very similar problem and eventually fixed it by exporting a 'dummy' class before my enum...

`

export class L{};

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

...

import {L, Legend} from '../../shared/legend'; `

Unfortunately I can't say why this was necessary (although I have seen remarks that enums without a class are not allowed why not?!) but it helped me.

Upvotes: 2

basarat
basarat

Reputation: 276085

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.

Upvotes: 3

Related Questions