Install node. Check: node --version Install npm. (comes as a package with node) npm --version Install angular cli: npm install -g @angular/cli Check: ng --version
ng new ng-ecommerce code . ng serve
Create a firebase account. It's free and go to https://console.firebase.google.com Click on "Add Project" Give Project name : BG-Ecommerce Click on "Add Firebase to your web app". Copy the config properties from firebase project to our application's environment.ts and environment.prod.ts file.
npm i --save firebase angularfire2 check versions in package.json ("angularfire2": "^5.1.2", "firebase": "^5.10.0")
npm i --save bootstrap Imort in styles.css
https://getbootstrap.com/docs/4.3/examples/starter-template/
ng g c navbar --dry-run ng g c navbar
common: front page, cell phones page, cart page, checkout page, thank you page ng g c common/front, ng g c common/cell-phones, ng g c common/cart, ng g c common/checkout, ng g c common/thank-you user: My purchases page, ng g c user/my-purchases seller: cell phones page, delivery page ng g c seller/seller-cell-phones, ng g c seller/delivery
RouterModule.forRoot([{ path: '', component: FrontComponent }]) routerLink, router-outlet
npm i --save @ng-bootstrap/ng-bootstrap We included just bootstrap css so far. But for the js components we need to install another package called ng-bootstrap. This has directives for generating bootstrap components like dropdown items etc that can be used with angular.
npm i -g firebase-tools check: firebase --version Open command line of windows. Go to D:/projects/ng-ecommerce/ firebase signin from Windows command line. (Git bash does not work) It will open a prompt in browser. signin with your gmail account. firebase init select hosting from the options and hit enter. Enter dist/ng-ecommerce as the directory for deployment instead of public enter No for rewriting index.html options Make sure it is reflected in firebase.json file.
Build project. With npm build --prod Deploy to firebase: firebase deploy Check the deployed app at https://ng-ecommerce-42b67.firebaseapp.com/
https://console.firebase.google.com For project ng-ecommerce Select Authentication Enable Google Authentication
Create signin component ng g c common/signin
After signin in user details with email and user id get saved in firebase under users information
AngularFireAuth .auth.signOut()
user.displayName
Any observable we are subscribing to should be unsubscribed. Solutions: 1. Call unsubscribe method on onDestroy lifecycle of component or 2. Instead of subscribing to the observable, declare it in component and then use it's value with async pipe in component.html. (Best solution)
So far we added the sign in and sign out service in component. But it makes things difficult while testing. So better to avoid tight coupling and move service to a new file thus, while testing we can fake these auth services instead of providing actual data ng g s services/auth Add the service in providers for app.module
canActivate, router.navigate, .pipe(map)
RouterStateSnapshot & query params IMPORTANT: Note that subscribe events in app.component.ts does not need to be unsubscribed becaused everything on app.component loads only once with our app. But for the dynamic components inside our app that can get called multiple times while we navigate, we should unsubscribe any existing observables. Otherwise it keeps on getting subscribed multiple times thus causing MEMORY LEAKS!!!
https://console.firebase.google.com Go to database url mentioned in environment.ts https://ng-ecommerce-42b67.firebaseio.com Go to Rules: { "rules": { ".read": "auth != null", ".write": "auth != null" } } Change the authentication rules so that we can access the database from our Angular app. After signing in, you can check that the data is now stored in database.
Go to database, users, under user id add new property isSeller: true To differentiate between normal user and a seller
ng g s services/seller-auth-route-guard
provide SellerAuthRouteGuardService in provider for app.module.ts After adding the route guard, modify isSeller in database. We won't be able to navigate to seller pages
Note that we have nested observables for switchMap and map in seller route guard and auth service. Thus async pipe in navar html will create an infinite loop to hang.
of from rxjs
Build project. With npm build --prod (Note that we got an error here for navbar component where private var was declared. changed to public) Deploy to firebase: firebase deploy Check the deployed app at https://ng-ecommerce-42b67.firebaseapp.com/
Create brands in firebase db. (following db.json file) Create brand service and add it to providers for app.module.ts
Querying brands from firebase
use ngModel directive after importing FormsModule in app.module.ts
use database.list('/cellphones').push(cellphone) and check database after saving
required, #var = "ngModel", #var.touched && #var.invalid
https://github.com/yuyang041060120/ng2-validation npm install ng2-validation --save
Use two way binding with template variables for inputs
this.database.list('/cellphones').valueChanges(); and ngFor with async pipes