lucasbauche / MyAngularLibrary

A basic example of a component library which can be consumed in Angular CLI projects. It includes an automatic build task using gulp.
MIT License
11 stars 6 forks source link

How to use MyNotificationComponent #1

Closed timofeysie closed 7 years ago

timofeysie commented 7 years ago

Do you have an example of how to use the MyNotificationComponent in a separate project? For example, should the MyLibrary module be declared in the declarations or imports arrays of the project using the lib, or just an import in the app.module.ts file, or only in the component that will use the tag?

I'm getting Module not found , Unexpected module and not a known element errors trying to use my version on this lib in another project.

timofeysie commented 7 years ago

The way to use the MyNotificationComponent is add it to the app.module.ts file of the project you want to use it in:

import * as quad from 'mylibrary/lib/components/notification.component';
@NgModule({
  declarations: [
    mylibrary.NotificationComponent

The notification component has a title and content.
These variables need to be created in the class. (there is no need to import the class here):

export class Page {
  title: string;
  content: string;
  constructor() {
      this.title = 'Hi there';
      this.content = 'You have notifications.';
  }

And in the template:

<my-notification [title]="title" [content]="content"></my-notification>

To use a simple class, such as a data model, for example a class called Cat in cat.ts, import the part that you want:

import { Cat } from 'my-library';

You can then create a member variable like this:

cat: Cat;

If your interested in taking this further checkout out Component Inheritance in Angular 2.