Submodule 23.1: Prepration
Site: | ΕΛ/ΛΑΚ Moodle |
Course: | Study / Web Design and Web Development |
Book: | Submodule 23.1: Prepration |
Printed by: | Guest user |
Date: | Sunday, 24 November 2024, 1:21 PM |
Introduction
The next step in this course is to store data in the db.json file in our json-server.
These data will be rendered in the HTML view every time we will call the page from our browser.
Let's prepare this page!
We will follow the same procedure that we used in the previous module. Follow the links provided on each page to remember how to accomplice the procedures.
Adding the Contact component
Exercise
- Create a new component named
contact
- Update the
routes.ts
file by importing the new component and adding the pathcontactus
for the new component - Update the Links in the
footer.component.html
file
In this page, we will create a form. When a user clicks the Submit button, the data of this form will be stored in the db.json
file in the feedback array
as objects (have a look at the end of your db.json file).
So, we have to create an Angular Service to send and fetch data between our application and our server.
Adding a feedback Service
Exercise
- Create a new
class named Feedback
in afile named feedback.ts
in theshared folder
and include in the class the necessary name/value pairs. You can see these in the db.json file (firstname , lastname , message
) - Import the class into the
contact.component.ts
file - Create a new service named
feedback
in the foldersrc\app\services
- Import the class into the
feedback.service.ts
file - Then add the service to the
app.module.ts
file - Complete the service!Import the
Observable
, theHttpClient
, thebaseURL
and update the constructor. Add agetFeedbacks()
method to fetch the data from the server.
The results
With the previous code configured, open your Terminal, go to the jsonServer
sub-folder and run json-server --watch db.json -d 2000
. This will introduce a delay of 2 seconds before the server sends the reply for the request. The server will serve the db.json file for your application. You can watch the server in your browser URL localhost:3000
Open another Terminal window, go to the angularChocoYourname
angular-sub-folder and run ng serve
to run your application in development mode. In your browser navigate to the URL localhost:4200
The contact page has been served by the json-server.
In the next submodule, you will create a form into this page.
Do a Git commit with the message "Fetching data".