Samy Sammour
Samy Sammour

Reputation: 2465

Angular Library failed to export class - Angular6

I have created a new Angular 6 Library. This Library has a Model called User:

export class User {
    public id: string;
    public username: string;

    constructor(id: string,
                username: string) {
        this.id = id;
        this.username = username;
    }
}

I am using the library in another angular app, everything works fine. But when I try to use the model:

export class AppComponent {
  public user: User;

  constructor() {
    this.user = new User('1', 'my user');
  }
}

I am getting this error:

Module not found: Error: Can't resolve 'my-lib/lib/models/user.model' in 'C:\my-app\src\app'

The intellisense found it but I am still receiving this error.

AppModule:

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    UserModule,
  ],
  bootstrap: [AppComponent]
})

anybody has an idea? Thanks!

Upvotes: 3

Views: 3559

Answers (1)

Philipp Meissner
Philipp Meissner

Reputation: 5482

The problem is that you did not export the model to the outside world of your library. In order to do that I suggest an additional file called public_api.ts in which you export the model explicitly. Place it right into the root of your library my-lib.

Then, fill the file up with explicit exports like so:

export * from './lib/models/user.model'

That should do it.

Upvotes: 4

Related Questions