Skip to content

Commit b1e9e7a

Browse files
committed
feat(@schematics/angular): add skipProvidedIn option to service schematic
Add a new `skipProvidedIn` boolean option to the service schematic that allows developers to generate services without the `providedIn: 'root'` metadata in the @Injectable decorator. When set to true, the decorator will be rendered as `@Injectable()` instead of `@Injectable({ providedIn: 'root' })`. This provides flexibility for developers who prefer to manually provide services in specific modules or components rather than using the default tree-shakeable pattern. The option defaults to false to maintain backward compatibility and continue encouraging Angular's best practice of tree-shakeable services. fixes #31670
1 parent 6ba8e07 commit b1e9e7a

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { Injectable } from '@angular/core';
22

3-
@Injectable({
3+
<% if (!skipProvidedIn) { %>@Injectable({
44
providedIn: 'root',
5-
})
6-
export class <%= classifiedName %> {
7-
8-
}
5+
})<% } else { %>@Injectable()<% } %>
6+
export class <%= classifiedName %> {}

packages/schematics/angular/service/index_spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,22 @@ describe('Service Schematic', () => {
121121
expect(content).toContain('export class FooService {');
122122
expect(testContent).toContain("describe('FooService', () => {");
123123
});
124+
125+
it('should allow skipping providedIn when skipProvidedIn is true', async () => {
126+
const options = { ...defaultOptions, skipProvidedIn: true };
127+
128+
const tree = await schematicRunner.runSchematic('service', options, appTree);
129+
const content = tree.readContent('/projects/bar/src/app/foo/foo.ts');
130+
expect(content).not.toMatch(/providedIn/);
131+
expect(content).toMatch(/^@Injectable\(\)$/m);
132+
});
133+
134+
it('should include providedIn: "root" by default', async () => {
135+
const options = { ...defaultOptions };
136+
137+
const tree = await schematicRunner.runSchematic('service', options, appTree);
138+
const content = tree.readContent('/projects/bar/src/app/foo/foo.ts');
139+
expect(content).toMatch(/providedIn: 'root'/);
140+
expect(content).toMatch(/^@Injectable\({$/m);
141+
});
124142
});

packages/schematics/angular/service/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
"description": "Skip the generation of a unit test file `spec.ts` for the service.",
4141
"default": false
4242
},
43+
"skipProvidedIn": {
44+
"type": "boolean",
45+
"default": false,
46+
"description": "When true, does not add the providedIn property to the @Injectable decorator. The service must then be provided manually"
47+
},
4348
"type": {
4449
"type": "string",
4550
"description": "Append a custom type to the service's filename. For example, if you set the type to `service`, the file will be named `my-service.service.ts`."

0 commit comments

Comments
 (0)