Skip to content

hohowin/angular-crash

Repository files navigation

angular-crashcourse


Setup

Global Installation

npm install -g @angular/cli

New App

ng new angular-crash

Dev Server

Angular Dev Server

yarn start

Then go to localhost:4200

Json Dev Server

yarn server

Development

New Component

ng generate component components/header
ng generate component components/button
ng generate component components/tasks
ng generate component components/task-item
ng generate component components/add-task

ng generate service services/task
ng generate service services/ui

# Local Server (not for production)
npm install json-server
# In Package Script add
# "server": "json-server --watch db.json"
# After started the server, can try http://localhost:5000/tasks

Font Awesome

ng add @fortawesome/angular-fontawesome

Add Modules

Add HttpClientModule and FormsModule. E.g: In app.module.ts add following:

import { FormsModule } from '@angular/forms';

 imports: [
    :
    :
    FormsModule,
  ],

Input & Output

[] are for input; () are for output; [()] are for bi-directional data binding.

Route

Add RouterModule and Routes in app.module.ts

import { RouterModule, Routes } from '@angular/router';
:
:
const appRoutes: Routes = [
  {path: '', component: TasksComponent },
  {path: 'about', component: AboutComponent },
  {path: 'footer', component: FooterComponent }
];
:
:
RouterModule.forRoot(appRoutes, {enableTracing: true}),

In app.component.html, add <router-outlet></router-outlet> instead of <app-tasks></app-tasks>

In about.component.html, add <a routerLink="/">Go Back</a>

Environment Variables

1. Create environment files under src

2. In task.service.ts, use environment.domain instead.

3. In angular.json, update the configurations and serve section correspondingly.

4. Update package.json scripts.

Dev Docker

For frontend:

docker run --rm -p 4200:4200 $(docker build -q -f Dockerfile.dev .)

For backend, go to json-server dir:

docker run --rm -p 5000:5000 $(docker build -q .)

OR

docker-compose up -d

And then go to http://localhost:4200


Production

Nginx

In nginx.conf, the following block redirects all /tasks traffic to http://json-backend:5000

 location /tasks {
   proxy_pass http://json-backend:5000;
 }

Start Server

docker-compose -f docker-compose-prod.yaml up -d --build 

Try it out

Try:

  • delete task
  • toggle task
  • add task

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published