Provide ContactService via a module

master
Kevin C. Coram 2019-09-04 22:12:04 -04:00
parent c95734bb04
commit 6d19f3fe97
Signed by: kevin
GPG Key ID: 0342351B3D61AD35
6 changed files with 35 additions and 6 deletions

View File

@ -5,11 +5,12 @@ import { AppComponent } from './app.component';
import { RouterTestingModule } from '@angular/router/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { NameComponent } from './name/name.component';
import { ContactModule } from '@nested-forms/contact';
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule, ReactiveFormsModule],
imports: [RouterTestingModule, ReactiveFormsModule, ContactModule.forRoot()],
declarations: [AppComponent, NameComponent, AddressListComponent, AddressComponent]
}).compileComponents();
}));

View File

@ -1,5 +1,4 @@
import { ContactService } from './../../../../libs/contact/src/lib/contact.service';
import { Contact } from '@nested-forms/contact';
import { Contact, ContactService } from '@nested-forms/contact';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { FormGroup, FormBuilder } from '@angular/forms';
import { Subscription } from 'rxjs';

View File

@ -7,6 +7,7 @@ import { ReactiveFormsModule } from '@angular/forms';
import { NameComponent } from './name/name.component';
import { AddressListComponent } from './address-list/address-list.component';
import { AddressComponent } from './address/address.component';
import { ContactModule } from '@nested-forms/contact';
@NgModule({
declarations: [AppComponent, NameComponent, AddressListComponent, AddressComponent],
@ -14,6 +15,7 @@ import { AddressComponent } from './address/address.component';
BrowserModule,
RouterModule.forRoot([], { initialNavigation: 'enabled' }),
ReactiveFormsModule,
ContactModule.forRoot(),
],
providers: [],
bootstrap: [AppComponent]

View File

@ -1,2 +1,3 @@
export * from './lib/contact-model';
export * from './lib/contact.service';
export * from './lib/contact.module';

View File

@ -0,0 +1,28 @@
import { NgModule, Optional, SkipSelf } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ContactService } from './contact.service';
@NgModule({
declarations: [],
imports: [
CommonModule
]
})
export class ContactModule {
constructor(
@Optional()
@SkipSelf()
parentModule: ContactModule
) {
if (parentModule) {
throw new Error('ContactModule is already loaded. Import it in the AppModule only');
}
}
static forRoot() {
return {
ngModule: ContactModule,
providers: [ ContactService ]
}
}
}

View File

@ -27,9 +27,7 @@ const contact: Contact = {
],
};
@Injectable({
providedIn: 'root'
})
@Injectable()
export class ContactService {
constructor() { }