Angular Interview Questions
Angular
FrontendWeb DevelopmentQuestion 9
How do you create a new Angular project using Angular CLI?
Answer:
Creating a new Angular project using Angular CLI is a straightforward process that involves a few simple commands. Angular CLI (Command Line Interface) is a powerful tool that simplifies various development tasks, such as setting up a new project, generating components, services, and other code scaffolding.
Here’s a step-by-step guide to creating a new Angular project using Angular CLI:
1. Install Angular CLI:
- Command: First, you need to install Angular CLI globally on your machine using npm (Node Package Manager). Open your terminal or command prompt and run:
npm install -g @angular/cli
- Purpose: This command installs the Angular CLI globally, making the
ng
command available anywhere on your system.
2. Create a New Project:
- Command: Once Angular CLI is installed, you can create a new Angular project by running:
ng new project-name
- Details:
project-name
: Replace this with the desired name of your project.- The CLI will prompt you to select additional options, such as adding Angular routing and the stylesheet format (CSS, SCSS, Sass, Less, etc.).
3. Navigate to the Project Directory:
- Command: Change directory to the newly created project folder:
cd project-name
4. Serve the Application:
- Command: To serve the application and see it in action, run:
ng serve
- Purpose: This command compiles the application and starts a development server. By default, the application will be accessible at
http://localhost:4200/
. - Details: The
ng serve
command watches for file changes and automatically reloads the application in the browser.
Example Workflow:
- Install Angular CLI:
npm install -g @angular/cli
- Create a New Project:
ng new my-angular-app
- During this step, you might see prompts like:
Would you like to add Angular routing? (y/N)
Which stylesheet format would you like to use? (CSS/SCSS/Sass/Less)
- During this step, you might see prompts like:
- Navigate to the Project Directory:
cd my-angular-app
- Serve the Application:
ng serve
Additional Details:
- Configuration Options: During project creation, Angular CLI configures the workspace with a default project structure, a set of configuration files, and essential dependencies.
- Development Server: The development server supports live-reloading, which means any changes made to the source files will automatically trigger a rebuild and reload the application in the browser.
- Further Development: You can generate additional components, services, modules, and other Angular constructs using CLI commands, such as
ng generate component component-name
orng generate service service-name
.
In Summary:
Creating a new Angular project using Angular CLI involves installing the CLI tool, running a command to set up the project, navigating to the project directory, and starting the development server. This streamlined process helps developers quickly scaffold and start working on new Angular applications.