Pratik
Pratik

Reputation: 755

How to export simple TypeScript class in Angular 4 module?

I have a simple TypeScript class that I want to package with a module and eventually export that module as a library using ng-packagr.

e.g. My class definition is -

export class Params {
    language: string ;
    country: string ;
    variant:  string ;
    dateFormat:  string ;
    briefDateTimeFormat:  string ;
    decimalFormat:  string ;
}

e.g. My module definition is -

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Params } from '../params/params';
import { LoginService } from '../sso/login.service';

@NgModule({
  imports: [CommonModule],
  exports: [Params],
  providers: [LoginService]
  declarations: [Params, LoginService]
})
export class FoundationModule { }

But when I try to export the FoundationModule in public-api.ts of ng-packagr, I get the following error -

BUILD ERROR
: Unexpected value 'Params in .../params/params.ts' declared by the module 'FoundationModule' in '.../foundation.module.ts'. Please add a @Pipe/@Directive/@Component annotation.

How can I package a single class in the Angular module?

Upvotes: 1

Views: 12274

Answers (2)

Nestor
Nestor

Reputation: 694

The two options.

export class Params {
language: string;
country: string;
variant: string;
dateFormat: string;
briefDateTimeFormat: string;
decimalFormat: string;
constructor(obj: any) {
    this.language = obj && obj.language || null;
    this.country = obj && obj.country || null;
    this.variant = obj && obj.variant || null;
    this.dateFormat = obj && obj.dateFormat || null;
    this.briefDateTimeFormat = obj && obj.briefDateTimeFormat || null;
    this.decimalFormat = obj && obj.decimalFormat || null;
}

}

or

export interface Params {
    language: string;
    country: string;
    variant: string;
    dateFormat: string;
    briefDateTimeFormat: string;
    decimalFormat: string;
}

and as the first answer says, the classes or interfaces doesn't go on the module.

Stackoverflow reference

Angular reference

Upvotes: 1

Z. Bagley
Z. Bagley

Reputation: 9260

The problem is in your module definition. You should directly import Params into each component/service where the class is required.

public-api.ts

import { Params } from '../params/params';

...
newVariable: Params = {
  language: 'my language',
  ...
}

Upvotes: 0

Related Questions