Skip to content

Commit 7fb59ea

Browse files
crisbetoclydin
authored andcommitted
fix(@schematics/angular): use service decorator in ng generate
Updates the `service` schematic in `ng generate` to use the `@Service` decorator instead of `@Injectable`. There's also a new `--injectable` flag to get back the old behavior.
1 parent b2f7a03 commit 7fb59ea

3 files changed

Lines changed: 21 additions & 4 deletions

File tree

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Injectable } from '@angular/core';
1+
import { <%= injectable ? 'Injectable' : 'Service' %> } from '@angular/core';
22

3-
@Injectable({
3+
<% if (injectable) { %>@Injectable({
44
providedIn: 'root',
5-
})
5+
})<% } else { %>@Service()<% } %>
66
export class <%= classifiedName %> {
77

88
}

packages/schematics/angular/service/index_spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ describe('Service Schematic', () => {
3636
skipPackageJson: false,
3737
};
3838
let appTree: UnitTestTree;
39+
3940
beforeEach(async () => {
4041
appTree = await schematicRunner.runSchematic('workspace', workspaceOptions);
4142
appTree = await schematicRunner.runSchematic('application', appOptions, appTree);
@@ -50,12 +51,23 @@ describe('Service Schematic', () => {
5051
expect(files).toContain('/projects/bar/src/app/foo/foo.ts');
5152
});
5253

53-
it('service should be tree-shakeable', async () => {
54+
it('should use @Service decorator', async () => {
5455
const options = { ...defaultOptions };
5556

5657
const tree = await schematicRunner.runSchematic('service', options, appTree);
5758
const content = tree.readContent('/projects/bar/src/app/foo/foo.ts');
59+
expect(content).toMatch(/@Service\(\)/);
60+
expect(content).toMatch(/import \{ Service \} from '@angular\/core'/);
61+
});
62+
63+
it('should use @Injectable decorator when injectable flag is true', async () => {
64+
const options = { ...defaultOptions, injectable: true };
65+
66+
const tree = await schematicRunner.runSchematic('service', options, appTree);
67+
const content = tree.readContent('/projects/bar/src/app/foo/foo.ts');
68+
expect(content).toMatch(/@Injectable\(/);
5869
expect(content).toMatch(/providedIn: 'root',/);
70+
expect(content).toMatch(/import \{ Injectable \} from '@angular\/core'/);
5971
});
6072

6173
it('should respect the skipTests flag', async () => {

packages/schematics/angular/service/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
"type": "boolean",
4949
"default": true,
5050
"description": "When true, the 'type' option will be appended to the generated class name. When false, only the file name will include the type."
51+
},
52+
"injectable": {
53+
"type": "boolean",
54+
"default": false,
55+
"description": "When true, generates an `@Injectable` instead of `@Service`."
5156
}
5257
},
5358
"required": ["name", "project"]

0 commit comments

Comments
 (0)