Theory
Full Stack નો front end
આ subject એક આખી app શરૂથી અંત સુધી બનાવે છે: FestConnect Full Stack, જેમાં Angular front end, Express/Node backend, MongoDB database અને auth તથા hosting માટે Firebase છે. આપણે ત્યાંથી શરૂ કરીએ છીએ જ્યાં user ખરેખર જુએ છે: front end, જે Angular થી બનેલું છે.
તમે Sem 5 માં Angular ને મળી ચૂક્યા છો, એટલે આ એવો recap છે જે વધુ ઊંડે જાય છે. આ પાઠ Angular ના ત્રણ core building blocks ફરી સ્થાપિત કરે છે, એટલે કે components, services અને routing, અને Angular 17 માં standalone components વાપરીને એમને સાથે જોડવાની આધુનિક રીત શીખવે છે. આ પાયો સ્પષ્ટ હોય તો બાકીનું આખું front-end unit સરળ બની જાય છે.
Theory
Components, services, routing
Angular એ single-page applications (SPAs) બનાવવા માટેનું TypeScript framework છે, એટલે એવી apps જે એક વાર load થાય છે અને આખું page reload કર્યા વગર view બદલતી રહે છે. ત્રણ concepts મોટા ભાગનું કામ કરે છે.
Component એ ફરી વાપરી શકાય એવો UI નો ટુકડો છે: HTML template અને styles સાથે જોડાયેલો એક TypeScript class (દાખલા તરીકે, EventCard). Services વહેંચાયેલું logic અને data સાચવે છે (જેમ કે events લાવતી EventService), અને Angular નું dependency injection માગનાર કોઈ પણ component ને એ service પહોંચાડી દે છે. Routing URL paths ને components સાથે જોડે છે, એટલે /events લખતાં events નું page ખૂલે છે અને single page છોડ્યા વગર navigation મળે છે. જોવા માટે components, logic માટે services, ફરવા માટે routing.
Practical
Angular 17 નો standalone component
import { Component } from '@angular/core';
@Component({
selector: 'app-event-card',
standalone: true, // Angular 17: no NgModule needed
template: `<div class="card">{{ title }}</div>`,
})
export class EventCardComponent {
title = 'Robotics Workshop';
}
// A service holds shared logic, injectable anywhere:
// @Injectable({ providedIn: 'root' })
// export class EventService { getEvents() { /* ... */ } }Formula
Angular 17: standalone components
સૌથી મોટો આધુનિક ફેરફાર છે standalone components. જૂનું Angular components ને NgModules માં જૂથબદ્ધ કરતું હતું; Angular 17 component ને standalone: true તરીકે ચિહ્નિત કરવાનું પસંદ કરે છે, જેથી એ પોતાની dependencies જાતે જાહેર કરે અને એને કોઈ module ની જરૂર જ ન રહે. આનાથી projects સરળ બને છે અને સમજવામાં આસાન રહે છે.
App bootstrapApplication(AppComponent, ...) વડે શરૂ થાય છે, અને routing જેવી સુવિધાઓ module imports ને બદલે providers દ્વારા ઉમેરાય છે (દાખલા તરીકે provideRouter(routes)). ઓછા હલનચલન કરતા ભાગ, વધુ સ્પષ્ટ માળખું: standalone એ જ દિશા છે જ્યાં Angular જઈ રહ્યું છે, અને FestConnect માં તમે આખો વખત એ જ વાપરવાના છો.
Practical
CLI વડે scalable project ઊભો કરવો
# Angular CLI project નું માળખું બનાવે અને સંભાળે છે
npm install -g @angular/cli
ng new festconnect # નવો Angular project બનાવે છે
cd festconnect
ng generate component event-card # component નું માળખું રચે છે
ng serve # http://localhost:4200 પર locally ચલાવો
# CLI સમજદાર folder structure આપે છે: code માટે src/app,
# અને project વધે તેમ components અને services feature folders માં ગોઠવાય છે.Quiz
Angular 17 માં, જૂના Angular ની સરખામણીએ components ગોઠવવાની રીતમાં મુખ્ય ફેરફાર કયો છે?
- Components હવે TypeScript વગર સાદા HTML માં લખાય છે
- Components standalone હોઈ શકે છે (standalone: true), જે NgModule ની જરૂર વગર પોતાની dependencies જાહેર કરે છે
- Routing હવે શક્ય રહ્યું નથી
- Framework માંથી services કાઢી નાખવામાં આવી છે
Show the answer
Components standalone હોઈ શકે છે (standalone: true), જે NgModule ની જરૂર વગર પોતાની dependencies જાહેર કરે છે
Angular 17 standalone components ને પસંદ કરે છે: standalone: true તરીકે ચિહ્નિત થયેલો component પોતાની dependencies જાતે જાહેર કરે છે અને એને કોઈ NgModule માં નોંધાવવો પડતો નથી, જેથી માળખું સરળ બને છે. વિકલ્પ A ખોટો છે: Angular ના components હજુ પણ HTML template સાથેના TypeScript classes જ છે, સાદું HTML નહીં. વિકલ્પ C ખોટો છે: routing પૂરેપૂરું ટકી રહ્યું છે, ફક્ત હવે એ routing module ને બદલે provideRouter જેવા providers દ્વારા ગોઠવાય છે. વિકલ્પ D ખોટો છે: dependency injection સાથેની services આજે પણ Angular નું કેન્દ્ર છે. યાદ રાખવાનો મુદ્દો એ કે જૂની NgModule આધારિત ગોઠવણની જગ્યાએ હવે standalone components આવ્યા છે.
Think first
Angular standalone components તરફ કેમ વળ્યું?
NgModules વર્ષો સુધી બરાબર કામ કરતા રહ્યા. તો Angular 17 એમને બદલે standalone components કેમ પસંદ કરે છે? વિચારીને પછી tap કરો.
Show the answer
કારણ કે NgModules એક એવું વધારાનું પડ અને આડકતરાપણું ઉમેરતા હતા જે નવા શીખનારાઓને, અને ઘણી વાર અનુભવી developers ને પણ, ગૂંચવતું હતું; standalone components એ પડ કાઢી નાખે છે. જૂની પદ્ધતિમાં દરેક component ને કોઈક NgModule માં જાહેર કરવો પડતો, અને કોઈ component કે feature વાપરવી હોય તો modules વચ્ચે યોગ્ય વસ્તુઓ import અને export થઈ છે કે નહીં એની ખાતરી કરવી પડતી. આમાંથી જ 'component is not a known element' જેવી કંટાળાજનક ભૂલો જન્મતી, અને આ બધો માનસિક બોજ તમે જે UI બનાવતા હતા એની સાથે ખાસ સંબંધ ધરાવતો નહોતો.
Standalone components દરેક component ને પોતાના જ decorator માં, ત્યાં ને ત્યાં, પોતાની જરૂરિયાતો (પોતાના imports) જાહેર કરવા દે છે. એટલે શીખવા માટે એક concept ઓછો રહે છે અને જોડાણ ખોટું પડવાની એક જગ્યા પણ ઓછી થાય છે. પરિણામે માળખું વધુ સપાટ અને સીધું બને છે: તમે એક જ component ની file જોઈને એની dependencies સમજી શકો છો, modules માં શોધખોળ કર્યા વગર.
આનાથી framework શીખવું પણ સહેલું બને છે (કંઈક બનાવતાં પહેલાં ઓછા concepts શીખવા પડે), tooling અને lazy loading સુધરે છે, અને Angular બીજા આધુનિક frameworks ની નજીક આવે છે. NgModules સુસંગતતા માટે હજુ હયાત છે, પણ હવે પછી standalone જ ભલામણ કરેલો default છે. મુખ્ય સૂર છે સરળીકરણ: જે વિધિ પોતાનું મૂલ્ય સાબિત ન કરી શકી એને દૂર કરવી. એટલે જ આ recap તમને FestConnect માટે સીધા standalone components થી શરૂ કરાવે છે. ઓછું જોડાણ, વધુ સ્પષ્ટ components.
Summary
Key takeaways
- Angular એ single-page applications માટેનું TypeScript framework છે; આ subject માં એ FestConnect ના front end માટે વપરાય છે.
- ત્રણ core building blocks: components (ફરી વાપરી શકાય એવા UI ટુકડા), services (વહેંચાયેલું logic અને data, injection થી મળતું) અને routing (URL paths ને components સાથે જોડવું).
- Dependency injection માગનાર કોઈ પણ component ને જરૂરી service પહોંચાડે છે.
- Angular 17 standalone components (standalone: true) પસંદ કરે છે, જે NgModule વગર પોતાની dependencies જાહેર કરે છે.
- App bootstrapApplication વડે શરૂ થાય છે અને providers દ્વારા ગોઠવાય છે (દા.ત. provideRouter).
- Angular CLI (ng new, ng generate, ng serve) scalable project નું માળખું રચે છે અને ચલાવે છે.
- યાદ રાખવાની કડી: components + services + routing, અને એમને જોડવાની આધુનિક રીત એટલે standalone components.