3

Trying to create library using ng-packagr.

Inside project I run :

ng g library nt-select --prefix nt

inside nt-select.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'nt-nt-select',
  template: `
    <mat-form-field>
      <mat-select [placeholder]="placeholder" [disabled]="disabled" [multiple]="multiple" [(ngModel)]="value"
                  [required]="required">

        <ngx-mat-select-search *ngIf="searchable"
                               [formControl]="optionFilterCtrl"
                               [placeholderLabel]="'Axtar'"
                               [noEntriesFoundLabel]="'Tapılmadı'"
                               [clearSearchInput]="false"
        ></ngx-mat-select-search>
        <mat-option *ngFor="let opt of filteredOptions$ | async" [value]="opt.value">
          {{opt.label}}
        </mat-option>
      </mat-select>

      <mat-error>{{getErrors('hobbies')}}</mat-error>
    </mat-form-field>

  `,
  styles: []
})
export class NtSelectComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

and nt.select.module:

import { NgModule } from '@angular/core';
import { NtSelectComponent } from './nt-select.component';
import {CommonModule} from '@angular/common';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {MatSelectModule} from '@angular/material';
import {NgxMatSelectSearchModule} from 'ngx-mat-select-search';

@NgModule({
  imports: [
    CommonModule,
    BrowserAnimationsModule,
    MatSelectModule,
    NgxMatSelectSearchModule,
    ReactiveFormsModule,
    FormsModule
  ],
  declarations: [NtSelectComponent],
  exports: [NtSelectComponent]
})
export class NtSelectModule { }

The problem is nt-select emits error cannot find name of mat-form-field, mat-select, ngx-mat-select-search even though I imported all modules.
Following tutorial on : https://blog.angularindepth.com/creating-a-library-in-angular-6-87799552e7e5
What might be issue ?
thanks in advance

1
  • did you have to add anything to the package.json for your library? wonder because i am having the same issue Commented Feb 19, 2019 at 17:42

1 Answer 1

2

Include angular material and other external packages in your ng-package.json file

{
  "lib": {
    "entryFile": "public_api.ts",
    "externals": {
      "@angular/material/input": "ng.material.input",
      "@angular/material/select": "ng.material.select"
      .....
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank so much! It solved the issue. Could you please tell me where I get information about this ? It worked but I have no idea why third-party libraries are imported in this way especially angular material.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.