Skip to content

Instantly share code, notes, and snippets.

@majo44
Last active December 12, 2019 13:12
Show Gist options
  • Save majo44/517cfe172221cda805535c88c48a7739 to your computer and use it in GitHub Desktop.
Save majo44/517cfe172221cda805535c88c48a7739 to your computer and use it in GitHub Desktop.
Angular modules vs simple functional modules
/////// ANGULAR
// app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { FeatureModule } from './feature.module.ts';
@NgModule({
declarations: [
// 1. declaring app component
AppComponent
],
imports: [
BrowserModule,
// 2. load routing module
AppRoutingModule,
// 3. load feature module
FeatureModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
// app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [];
@NgModule({
// 4. registering routes
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
// feature.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FeatureComponentComponent } from './feature-component.component';
@NgModule({
// 5.declaring feature component
declarations: [FeatureComponentComponent],
imports: [
CommonModule
]
})
export class FeatureModule { }
/////////// FUNCTIONAL
// app.module.ts
import { appRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { featureModule } from './feature.module.ts';
export const appModule = (deps) => {
// 1. declaring app component
define(deps.window, 'app-component, AppComponent, deps.someDeps /** projection of deps */);
// 2. load routing module
appRoutingModule(deps.router);
// 3. load feature module
featureModule(deps.someDeps /** projection of deps */);
}
// app-routing.module.ts
export const routingModule = (router: SomeRouter) => {
// 4. registering routes
router.addRoute(....
}
// feature.module.ts
import { FeatureComponentComponent } from './feature-component.component';
export const featureModule = (deps) => {
// 5.declaring feature component
define(deps.window, 'feature-component, FeatureComponentComponent, deps.someDeps /** projection of deps */);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment