ng generate service command in Angular CLI
ng generate service
command is used to create libraries in Angular applications
ng generate service
syntax
ng generate service
command takes one parameter i.e, name
Additionally we can pass different options as well.
ng generate service [name] [options]
or you can use shorthand syntax
ng generate s [name] [options]
ng generate service
arguments
ng generate service
command takes one argument i.e, name
.
- name : name of the service.
The name
type is string.
ng generate service
options
ng generate service
accepts 3 different types of options.
- flat
- project
- skip-tests
Option | Description | Type | Default Value |
---|---|---|---|
flat | When true (the default), creates files at the top level of the project. | boolean | true |
project | The name of the project. | string | |
skip-tests | Do not create "spec.ts" test files for the new service. | boolean | false |
ng generate service
example
For example if you want to create a service named product, use ng generate product
command.
CREATE src/app/product.service.spec.ts (362 bytes)
CREATE src/app/product.service.ts (136 bytes)