#0: Init photo gallery
Setup project
Let's start with creating new project - as you probably remember we can do it from terminal with command
Tip This tutorial presents scss styling so choose that option during project creation.
At the begining we want to just display photos in gallery. To create it we need one service (to manage our photos) and two components (photo
for displaying separate image and gallery
to display collection of photos).
Reminder: To create component we can use command
and to create service
Create Photo interface
Now, we have to define how our photo objects will look like - for each photo we need info about its id, url, descrption and optionally categoryID. So let's create an interface Photo by command:
Our Photo interface should look like this:
Photo service
Photos service will be responsible for all actions connected with management of photos. It starts with one field: photos
- an array with a bunch of example photos. So photos
in Photos service has type Photo[]
.
First weβre going to use it to display our gallery. Later, weβll gradually add Observables and interactvity, based on this array with photos.
We need some initial photos in our gallery so just copy-paste array from snippet below in your photo.service.ts
(above the class body).
and put it as initial value for photos
property (inside the PhotoService class)
Thanks to that we have simple list of photos we can present in gallery component.
Gallery component
Gallery Component refers to the list of photos, so we have to add corresponding photosList
property in gallery.component.ts
file:
Don't forget to inject the service in component's constructor like here:
Now you can display our list using *ngFor
in gallery.component.html
file:
Photo component
As you see thereβs one Component left to implement: app-photo
which displays an individual photo.
In the photo.component.ts
declare one Input
and use this data in HTML template
Add components to app view
We created a lot of code but nothing is visible in the bropwser preview! We can easily fix it just by adding gallery component in app.component.html
file
Styling
To make our app beautiful add following code to gallery.component.scss
Height property to :host and .photo-gallery-app in app.component.scss
and belowed snippet to photo.component.scss
Congrats! You've just prepared initial version of our gallery!
Last updated
Was this helpful?