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.

  1. name : name of the service.

The name type is string.

ng generate service options

ng generate service accepts 3 different types of options.

  1. flat
  2. project
  3. skip-tests

OptionDescriptionTypeDefault Value
flatWhen true (the default), creates files at the top level of the project.booleantrue
projectThe name of the project.string
skip-testsDo not create "spec.ts" test files for the new service.booleanfalse

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)
Avatar

Arunkumar Gudelli

Liked this post? Subscribe