-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNgModules-Examples
197 lines (159 loc) · 4.19 KB
/
NgModules-Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// NgModules Examples
// Example 1: Mock Environment Config
// app.ts
import { Component, Injectable, NgModule, Directive, Pipe, PipeTransform } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { RouterModule, Routes, Router } from '@angular/router';
import { BrowserModule } from '@angular/platform-browser';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { AngularFireModule } from '@angular/fire/compat';
import { AngularFireAuthModule } from '@angular/fire/compat/auth';
import { environment } from './environments/environment'; // Mock environment
// Mock Environment Config
const environment = {
firebaseConfig: {
apiKey: "mock-api-key",
authDomain: "mock-auth-domain",
projectId: "mock-project-id",
storageBucket: "mock-storage-bucket",
messagingSenderId: "mock-sender-id",
appId: "mock-app-id"
}
};
// Example 2: Root Module with Bootstrap
@Component({
selector: 'app-root',
template: `
<h1>Angular NgModules Demo</h1>
<nav>
<a routerLink="/feature">Feature Module</a>
<a routerLink="/shared">Shared Module</a>
<a routerLink="/core">Core Module</a>
<a routerLink="/lazy">Lazy Module</a>
</nav>
<router-outlet></router-outlet>
`
})
export class AppComponent {}
// Example 3: Component for Declarations
@Component({
selector: 'app-my-component',
template: `<p>My Component</p>`
})
export class MyComponent {}
// Example 4: Directive for Declarations
@Directive({
selector: '[appMyDirective]'
})
export class MyDirective {}
// Example 4: Feature Module
@Component({
selector: 'app-feature',
template: `
<h2>Feature Component</h2>
<p>This is a feature module</p>
`
})
export class FeatureComponent {}
@NgModule({
declarations: [FeatureComponent],
imports: [CommonModule],
})
export class FeatureModule {}
// Example 5: Shared Module
@Component({
selector: 'app-shared',
template: `<h2>Shared Component</h2>`
})
export class SharedComponent {}
@Pipe({
name: 'sharedPipe'
})
export class SharedPipe implements PipeTransform {
transform(value: string): string {
return value + ' (shared)';
}
}
@NgModule({
declarations: [SharedComponent, SharedPipe],
imports: [CommonModule],
exports: [SharedComponent, SharedPipe]
})
export class SharedModule {}
// Example 6: Core Module with Singleton Services
@Injectable()
export class LoggerService {
log(message: string) {
console.log(message);
}
}
@Injectable()
export class AuthService {
private loggedIn = false;
login() { this.loggedIn = true; }
isAuthenticated() { return this.loggedIn; }
}
@NgModule({
providers: [LoggerService, AuthService],
imports: [CommonModule]
})
export class CoreModule {}
// Example 7: Lazy-Loaded Module
@Component({
selector: 'app-lazy',
template: `
<h2>Lazy Component</h2>
<p>This is a lazy-loaded module</p>
`
})
export class LazyComponent {}
@NgModule({
declarations: [LazyComponent],
imports: [CommonModule]
})
export class LazyModule {}
// Example 8: Sharing Across Modules
@Component({
selector: 'app-shared-demo',
template: `
<h2>Shared Demo</h2>
<app-shared></app-shared>
<p>{{ 'Hello' | sharedPipe }}</p>
`
})
export class SharedDemoComponent {}
// Root NgModule (AppModule)
@NgModule({
declarations: [
AppComponent,
MyComponent,
MyDirective,
FeatureComponent,
SharedDemoComponent
],
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot([
{ path: 'feature', component: FeatureComponent },
{ path: 'shared', component: SharedDemoComponent },
{ path: 'core', component: AppComponent }, // Using root component for simplicity
{ path: 'lazy', loadChildren: () => Promise.resolve(LazyModule) }
]),
CoreModule,
SharedModule,
FeatureModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
// Bootstrap the Application
bootstrapApplication(AppComponent, {
providers: [
provideRouter([]), // Router is already provided by AppModule
{ provide: LoggerService, useClass: LoggerService },
{ provide: AuthService, useClass: AuthService }
]
}).catch(err => console.error(err));