CLI

The Angular CLI

[R] https://github.com/angular/angular-cli/wiki

a command line interface tool that can create a project, add files, and perform a variety of ongoing development tasks such as testing, bundling, and deployment.

Generate a new project and skeleton application

ng new my-app --routing
cd my-app
ng serve --open

The ng serve command launches the server, watches your files, and rebuilds the app as you make changes to those files.

the --open (or just -o) option will automatically open your browser on http://localhost:4200/.

ng generate

Component

ng generate component <component_name>
ng g c <component_name>

ng generate component module_name/heroes --spec=false (no spec file)
ng generate component heroes -it  (inline template)

Service

There are several ways to provide the HeroService: in the HeroesComponent, in the AppComponent, in the AppModule. Each option has pros and cons.

ng generate service hero
ng g s core/<service_name> --module=core

ng generate service hero

ng generate service core/hero --module=core
ng generate service hero --spec=false --module=app

Module

ng generate module <module_name>
ng g m <module_name>

ng g m share --routing --module=app
ng g m core --module=app
ng generate module app-routing --flat --module=app --routing

--flat puts the file in src/app instead of its own folder. --module=app tells the CLI to register it in the imports array of the AppModule.

Directives

ng generate directives <directive_name>
ng g d <directive_name>

ng g d Demo --spec=false

Last updated

Was this helpful?