Angular 9 Tutorial: Project Structure

In the previous post, we have discussed how to create hello world application in angular. Using Angular CLI, we have generated basic files needed to run a hello world application.

Now, let’s have a quick look at the files and folders in our new Angular project.

The generated projects have the following files and folders structure

The top level of the workspace contains workspace-wide configuration files, configuration files for the root-level application, and subfolders for the root-level application source and test files.

WORKSPACE CONFIG FILESPURPOSE
.editorconfigConfiguration for code editors. See EditorConfig.
.gitignoreSpecifies intentionally untracked files that Git should ignore.
README.mdIntroductory documentation for the root app.
angular.jsonCLI configuration defaults for all projects in the workspace, including configuration options for build, serve, and test tools that the CLI uses, such as TSLintKarma, and Protractor. For details, see Angular Workspace Configuration.
package.jsonConfigures npm package dependencies that are available to all projects in the workspace. See npm documentation for the specific format and contents of this file.
package-lock.jsonProvides version information for all packages installed into node_modules by the npm client. See npm documentation for details. If you use the yarn client, this file will be yarn.lock instead.
src/Source files for the root-level application project.
node_modules/Provides npm packages to the entire workspace. Workspace-wide node_modules dependencies are visible to all projects.
tsconfig.jsonDefault TypeScript configuration for projects in the workspace.
tslint.jsonDefault TSLint configuration for projects in the workspace.

Application source files

Files at the top level of src/ support testing and running your application. Subfolders contain the application source and application-specific configuration.

APP SUPPORT FILESPURPOSE
app/Contains the component files in which your application logic and data are defined. See details below.
assets/Contains image and other asset files to be copied as-is when you build your application.
environments/Contains build configuration options for particular target environments. By default there is an unnamed standard development environment and a production (“prod”) environment. You can define additional target environment configurations.
favicon.icoAn icon to use for this application in the bookmark bar.
index.htmlThe main HTML page that is served when someone visits your site. The CLI automatically adds all JavaScript and CSS files when building your app, so you typically don’t need to add any <script> or<link> tags here manually.
main.tsThe main entry point for your application. Compiles the application with the JIT compiler and bootstraps the application’s root module (AppModule) to run in the browser. You can also use the AOT compiler without changing any code by appending the --aot flag to the CLI build and serve commands.
polyfills.tsProvides polyfill scripts for browser support.
styles.sassLists CSS files that supply styles for a project. The extension reflects the style preprocessor you have configured for the project.
test.tsThe main entry point for your unit tests, with some Angular-specific configuration. You don’t typically need to edit this file.

Inside the src/ folder, the app/ folder contains your project’s logic and data. Angular components, templates, and styles go here.

SRC/APP/ FILESPURPOSE
app/app.component.tsDefines the logic for the app’s root component, named AppComponent. The view associated with this root component becomes the root of the view hierarchy as you add components and services to your application.
app/app.component.htmlDefines the HTML template associated with the root AppComponent.
app/app.component.cssDefines the base CSS stylesheet for the root AppComponent.
app/app.component.spec.tsDefines a unit test for the root AppComponent.
app/app.module.tsDefines the root module, named AppModule, that tells Angular how to assemble the application. Initially declares only the AppComponent. As you add more components to the app, they must be declared here.

Application configuration files

The application-specific configuration files for the root application reside at the workspace root level. For a multi-project workspace, project-specific configuration files are in the project root, under projects/project-name/.

Project-specific TypeScript configuration files inherit from the workspace-wide tsconfig.json, and project-specific TSLint configuration files inherit from the workspace-wide tslint.json.

APPLICATION-SPECIFIC CONFIG FILESPURPOSE
browserslistConfigures sharing of target browsers and Node.js versions among various front-end tools. See Browserslist on GitHub for more information.
karma.conf.jsApplication-specific Karma configuration.
tsconfig.app.jsonApplication-specific TypeScript configuration, including TypeScript and Angular template compiler options. See TypeScript Configuration and Angular Compiler Options.
tsconfig.spec.jsonTypeScript configuration for the application tests. See TypeScript Configuration.
tslint.jsonApplication-specific TSLint configuration.

Reference

https://angular.io/guide/file-structure

Course Main page

Angular 9 Tutorial for Beginners: Action Plan

Angular 9 Tutorial: Angular CLI

Angular CLI is one of the best tool available for angular to setup the initial files or scaffolding structure of the application.

Setup

Once you have setup the basic software like node.js and npm. You can download CLI using the following command

npm install -g @angular/cli

Once installed we can check the version of angular CLI using the following command

ng version

One common arguments every CLI has is help command. Here it is for you

ng help

Create new Angular App

From your target empty folder, you need to run the following command to generate the basic files/

ng new my-first-project

Make sure to replace ‘my-first-project’ with name you prefer. This is will your application name.

Angular CLI have lots of others option to help in setting the basic to advance files.

Available angular CLI commands

Available angular CLI commands By using command ng help, I’m getting the following information

  • add – Adds support for an external library to your project.
  • analytics – Configures the gathering of Angular CLI usage metrics. See https://v8.angular.io/cli/usage-analytics-gathering.
  • build (b) – Compiles an Angular app into an output directory named dist/ at the given output path. Must be executed from within a workspace directory.
  • deploy – Invokes the deploy builder for a specified project or for the default project in the workspace.
  • config – Retrieves or sets Angular configuration values in the angular.json file for the workspace.
  • doc (d) – Opens the official Angular documentation (angular.io) in a browser, and searches for a given keyword.
  • e2e (e) – Builds and serves an Angular app, then runs end-to-end tests using Protractor.
  • generate (g) – Generates and/or modifies files based on a schematic.
  • help – Lists available commands and their short descriptions.
  • lint (l) – Runs linting tools on Angular app code in a given project folder.
  • new (n) – Creates a new workspace and an initial Angular app.
  • run – Runs an Architect target with an optional custom builder configuration defined in your project.
  • serve (s) – Builds and serves your app, rebuilding on file changes.
  • test (t) – Runs unit tests in a project.
  • update – Updates your application and its dependencies. See https://update.angular.io/
  • version (v) – Outputs Angular CLI version.
  • xi18n (i18n-extract) – Extracts i18n messages from source code.

Creating new angular project

ng new timer-project

timer-project is name of project we chose.

The CLI asks us choose our required options based on our needs. Like routing and stylesheet type.

After installing package, we are good to go with debugging of the newly generated application. We can discuss about the project structure in our upcoming post.

You can checkout some of our other posts related to angular CLI

Serving the project

Angular CLI provides a complete tool-chain for developing front-end apps on your local machine. As such, you don’t need to install a local server to serve your project — you can simply, use the ng serve command from your terminal to serve your project locally.

ng serve

The application will start serving in the root http://localhost:4200

Generating angular schematic

Generates and/or modifies files based on a schematic.

ng generate <schematic> [options]
ng g <schematic> [options]

schematic can be replaced with following sets

  • appShell
  • application
  • class
  • component
  • directive
  • enum
  • guard
  • interceptor
  • interface
  • library
  • module
  • pipe
  • service
  • serviceWorker
  • webWorker

These commands is fair to start using angular CLI in beginner mode.

Happy Coding!

Course Main Page

Angular 9 Tutorial for Beginners: Action Plan